using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace UDP2 { public partial class Form1 : Form { private UdpClient sendUdpClient; private UdpClient receiveUpdCLient; public Form1() { InitializeComponent(); IPAddress[] ips = Dns.GetHostAddresses(""); textBox1.Text = ips[0].ToString(); int port = 5000; textBox2.Text = port.ToString(); textBox4.Text = ips[0].ToString(); textBox3.Text = port.ToString(); } private void textBox4_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void button4_Click(object sender, EventArgs e) { IPAddress localIp = IPAddress.Parse(textBox1.Text); IPEndPoint localIpEndPoint = new IPEndPoint(localIp, int.Parse(textBox2.Text)); receiveUpdCLient = new UdpClient(localIpEndPoint); Thread receiceThread = new Thread(ReceiveMessage); receiceThread.Start(); } //接受消息方法 private void ReceiveMessage() { IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);//获得连接到本机的远程机器的IPENDPOINT while (true) { try { //关闭receiveUdpClient时此时会产生异常 byte[] receiceBytes = receiveUpdCLient.Receive(ref remoteIpEndPoint); string message = Encoding.Unicode.GetString(receiceBytes); //显示消息内容 ShowMessageforView(listBox1, string.Format("{0}[{1}])", remoteIpEndPoint, message)); } catch { break; } } } //利用委托回调机制实现界面上消息内容显示 delegate void ShowMessageforViewCallBack(ListBox listbox, string text); private void ShowMessageforView(ListBox listbox, string text) { if (listbox.InvokeRequired) { ShowMessageforViewCallBack showMessageforViewCallback = ShowMessageforView; listbox.Invoke(showMessageforViewCallback, new object[] { listbox, text }); } else { listBox1.Items.Add(text); listBox1.SelectedIndex = listBox1.Items.Count - 1; listBox1.ClearSelected(); } } private void button1_Click(object sender, EventArgs e) { if (textBox5.Text == string.Empty) { MessageBox.Show("发送内容不能为空", "提示"); return; } //选择发送模式 if (checkBox1.Checked == true) { //匿名模式(套接字绑定的端口由系统随机分配) sendUdpClient = new UdpClient(0); } else { //实名模式(套接字绑定到本地指定的端口) IPAddress localIP = IPAddress.Parse(textBox1.Text); IPEndPoint localIpEndPoint = new IPEndPoint(localIP, int.Parse(textBox2.Text)); sendUdpClient = new UdpClient(localIpEndPoint); } Thread sendThread = new Thread(SendMessage); sendThread.Start(textBox5.Text); } //发送消息方法 public void SendMessage(object obj) { string message = (string)obj; byte[] sendbytes = Encoding.Unicode.GetBytes(message); IPAddress remoteIp = IPAddress.Parse(textBox4.Text); IPEndPoint remoteIpEndPoint = new IPEndPoint(remoteIp, int.Parse(textBox3.Text)); //显示消息内容 sendUdpClient.Send(sendbytes, sendbytes.Length, remoteIpEndPoint); sendUdpClient.Close(); ResetMessageText(textBox5); } delegate void ResetMessageCallback(TextBox textbox); private void ResetMessageText(TextBox textbox) { // Control.InvokeRequired属性代表 // 如果控件的处理与调用线程在不同线程上创建的,则为true,否则为false if (textbox.InvokeRequired) { ResetMessageCallback resetMessagecallback = ResetMessageText; textbox.Invoke(resetMessagecallback, new object[] { textbox }); } else { textbox.Clear(); textbox.Focus(); } } private void button3_Click(object sender, EventArgs e) { receiveUpdCLient.Close(); } private void button2_Click(object sender, EventArgs e) { this.listBox1.Items.Clear(); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } EndPoint ep; Socket socke; private void button1_Click(object sender, EventArgs e) { socke = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9095); //初始化 ep = (EndPoint)iep; socke.Bind(iep); Thread re = new Thread(ReceiveMessage); re.Start(socke); } private void ReceiveMessage(object o) { Socket socke = o as Socket; string msg; byte[] data = new byte[1024]; while (true) { try { // 关闭receiveUdpClient时此时会产生异常 int receiveBytes = socke.ReceiveFrom(data, ref ep); string message = Encoding.Unicode.GetString(data,0,receiveBytes); // 显示消息内容 ShowMessageforView(lstbxMessageView, string.Format("{0}[{1}]", ep, message)); } catch { break; } } } //接受消息 delegate void ShowMessageforViewCallBack(ListBox listbox, string text); private void ShowMessageforView(ListBox listbox, string text) { if (listbox.InvokeRequired) { ShowMessageforViewCallBack showMessageforViewCallback = ShowMessageforView; listbox.Invoke(showMessageforViewCallback, new object[] { listbox, text }); } else { lstbxMessageView.Items.Add(text); for (int i = 0; i < lstbxMessageView.Items.Count; i++) { if (lstbxMessageView.Items[i].ToString().Contains("172.16.5.58")) { lstbxMessageView.Items.Clear(); } } lstbxMessageView.SelectedIndex = lstbxMessageView.Items.Count - 1; lstbxMessageView.ClearSelected(); } } private void button2_Click(object sender, EventArgs e) { Thread sendThread = new Thread(SendMessage); sendThread.Start(textBox1.Text); } public void SendMessage(object obj) { string message = (string)obj; Socket sockes = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint iep=new IPEndPoint(IPAddress.Parse("172.16.255.255"),9095); sockes.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); //显示消息内容 byte[] sendbytes = Encoding.Unicode.GetBytes(message); sockes.SendTo(sendbytes, iep); sockes.Close(); } } }