发布时间:2020-02-27编辑:佚名阅读(1836)
程序界面
流程图
代码
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; using System.Net.Sockets; using System.Threading; namespace UDP通信程序 { public partial class UDPserverForm : Form { private UdpClient udpserver;//UDP服务器 private Thread udpListenThread;//UDP监听线程 private IPEndPoint remoteIpAndPort;//远程IP地址和端口 private delegate void displayMessageDelegate();//委托 public UDPserverForm() { InitializeComponent(); udpListenThread = new Thread(new ThreadStart(udpListen));//创建监听线程 udpListenThread.IsBackground = true;//设为后台线程 udpListenThread.Start();//启动监听线程 //↓ 显示本机内网IPv4地址 win7系统 //this.textBoxHostIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[3].ToString(); for(int i = 0; i < Dns.GetHostEntry(Dns.GetHostName()).AddressList.Length; i++) { //MessageBox.Show(Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].ToString()); //↓ 获取本机的IPv4地址,不知道怎么获取,只找到了这个办法 if (Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].AddressFamily.ToString().Equals("InterNetwork")) { this.textBoxHostIp.Text = Dns.GetHostEntry(Dns.GetHostName()).AddressList[i].ToString(); break; } } } private void udpListen()//监听,线程的实际代码 { int i = 1; while (true)//循环,端口不可用自动加1 { try { udpserver = new UdpClient(int.Parse(this.textBoxPortNumber.Text));//创建UDP服务器,绑定要监听的端口 break; } catch { //就是将预设的端口号加1 this.textBoxPortNumber.Text = (int.Parse(this.textBoxPortNumber.Text)+i++).ToString(); } } remoteIpAndPort = new IPEndPoint(IPAddress.Any, 0);//定义IPENDPOINT,装载远程IP地址和端口 string receivedStr;//保存接收的数据字符的临时变量 while (true) { try { //将udpserver接受到指定的远程主机的数据包转换成字符串保存在临时变量 receivedStr = System.Text.Encoding.UTF8.GetString(udpserver.Receive(ref remoteIpAndPort)); //定义委托 displayMessageDelegate dis = delegate() { string s = "\n[I received some data]"; byte[] b = System.Text.Encoding.UTF8.GetBytes(s); this.udpserver.Send(b, b.Length, remoteIpAndPort);//接收到数据后返回 “[I received some data]” this.textBoxRemoteIp.Text = remoteIpAndPort.Address.ToString();//远程主机的IP显示到窗体 this.textBoxRemotePort.Text = remoteIpAndPort.Port.ToString();//远程主机的端口号显示到窗体 this.richTextBoxRe.AppendText(string.Format("\n{0}", remoteIpAndPort));//显示远程回话(当昵称用) this.richTextBoxRe.AppendText("\n"+receivedStr);//数据报显示到接收区域 this.richTextBoxRe.ScrollToCaret();//滚动到最下面,显示最新消息 //↓ 更新显示本地端口号(无实际意义) this.textBoxPortNumber.Text = ((IPEndPoint)(this.udpserver.Client.LocalEndPoint)).Port.ToString(); }; this.Invoke(dis);//执行委托 } catch { break; } } } private void buttonClose_Click(object sender, EventArgs e) { Application.Exit(); } private void buttonSend_Click(object sender, EventArgs e) { try { //将发送框中的文本转化成byte数组 byte[] b = System.Text.Encoding.UTF8.GetBytes(this.richTextBoxSend.Text); //发送到指定的远程主机(这里是谁发消息到udpserver,它就发给谁) this.udpserver.Send(b, b.Length, remoteIpAndPort); this.richTextBoxRe.AppendText("\n" + this.textBoxHostIp.Text + ":" + this.textBoxPortNumber.Text); this.richTextBoxRe.AppendText("\n"+this.richTextBoxSend.Text); this.richTextBoxSend.Clear(); this.richTextBoxRe.ScrollToCaret(); } catch { ; } } //修改本地监听端口号则重启线程 private void buttonUpdatePortNumber_Click(object sender, EventArgs e) { udpserver.Close();//释放UDP连接 udpListenThread.Abort();//kill线程 //重建线程 udpListenThread = new Thread(new ThreadStart(udpListen)); udpListenThread.IsBackground = true; udpListenThread.Start(); } } }
上一篇:C#模拟键盘按键的三种方式
下一篇:et文件
1人
0人
0人
0人