using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) {//发送信息到UDP通信服务端 try { string MyIP = this.textBox1.Text; int MyPort = 8800; SendMessage(MyIP, MyPort); } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void SendMessage(string MyIP, int MyPort) {//消息发送方法 IPEndPoint MyRemoteEndPoint = new IPEndPoint(IPAddress.Parse(MyIP), MyPort); UdpClient MyClient = new UdpClient(); //MyClient.MulticastLoopback = false; Byte[] MyData = System.Text.Encoding.Default.GetBytes(this.richTextBox1.Text); MyClient.Send(MyData, MyData.Length, MyRemoteEndPoint); } private void button2_Click(object sender, EventArgs e) {//群发消息 try { string MyIP = "192.168.2.10"; int MyPort = 8800; SendMessage(MyIP, MyPort); } catch (Exception ex) { MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void Form1_Load(object sender, EventArgs e) { } } }、、、、、、、、
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); CheckForIllegalCrossThreadCalls=false; } UdpClient MyClient; Thread MyThread; private void ReceiveDataFromUDPClient() {//接收从UDP通信客户端发来的信息 //string MyIP = "192.168.2.188"; int MyPort = 8800; MyClient = new UdpClient(MyPort); while(true) { try { //IPEndPoint MyRemoteEndPoint = new IPEndPoint(IPAddress.Parse(MyIP), MyPort); IPEndPoint MyRemoteEndPoint=null;// = new IPEndPoint(IPAddress.Parse(MyIP), MyPort); Byte[] MyData=MyClient.Receive(ref MyRemoteEndPoint); string MyInfo=System.Text.Encoding.Default.GetString(MyData); this.richTextBox1.Text+="/n>>"+MyRemoteEndPoint.Address.ToString()+"::"+MyInfo; } catch(Exception ex) { this.richTextBox1.Text+="/n>>"+ex.Message; MessageBox.Show(ex.Message); } } } private void button1_Click(object sender, EventArgs e) {//创建UDP通信服务端接收信息 IPHostEntry hostName = Dns.GetHostEntry(Dns.GetHostName()); string MyIP = hostName.AddressList[0].ToString(); int MyPort = 8800; IPEndPoint MyRemoteEndPoint = new IPEndPoint(IPAddress.Parse(MyIP), MyPort); MyThread = new System.Threading.Thread(new ThreadStart(this.ReceiveDataFromUDPClient)); MyThread.IsBackground = true; MyThread.Start(); this.richTextBox1.Text += "/n>>UDP服务端开始接收从客户端发来的数据:"; } private void button3_Click(object sender, EventArgs e) { MyThread.Abort(); MyClient.Close(); } public UdpClient udpClient; public Thread UdpThread; private void button2_Click(object sender, System.EventArgs e) { //listBox1.Items.Add("启动..."); if (udpClient != null) { UdpThread.Abort(); Thread.Sleep(TimeSpan.FromMilliseconds(500d)); udpClient.Close(); } try { udpClient = new UdpClient(int.Parse(textBox1.Text)); UdpThread = new Thread(new ThreadStart(UdpReciveThread)); UdpThread.Start(); } catch (Exception y) { MessageBox.Show(this, y.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void UdpReciveThread() { IPEndPoint remoteHost = null; listBox1.Items.Add("启动..."); while (udpClient != null && Thread.CurrentThread.ThreadState.Equals(ThreadState.Running)) { try { listBox1.Items.Add("等待连接..."); byte[] buf = udpClient.Receive(ref remoteHost); string bufs = Encoding.UTF8.GetString(buf); string aaa = "主机:"+remoteHost.Address+"; 端口:"+remoteHost.Port+"; 数据报长度:"+buf.Length; listBox1.Items.Add(aaa); listBox1.Items.Add(bufs); } catch (Exception y) { listBox1.Items.Add(y.Message); listBox1.Items.Add(y.Source); } } listBox1.Items.Add("结束..."); } private void Form1_Load(object sender, EventArgs e) { } } }