在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1、主界面
2、源程序 Send public partial class formUdpSend : Form { //声明一个UdpClient对象 UdpClient udpClient; public formUdpSend() { InitializeComponent(); //实例化UdpClient对象 udpClient = new UdpClient(); } private void btnSend_Click(object sender, EventArgs e) { //获取发送信息 string message = this.txtMessage.Text; //获取发送数据 byte[] data = System.Text.Encoding.UTF8.GetBytes(message); udpClient.Connect(IPAddress.Parse(this.txtIP.Text),12345); udpClient.Send(data, data.Length); this.txtMessage.Text = ""; } } Receive public partial class formUdpReceive : Form { UdpClient udpClient; Thread thread; public formUdpReceive() { InitializeComponent(); //不捕获对错误线程的调用 CheckForIllegalCrossThreadCalls = false; udpClient = new UdpClient(12345); } //设置监听 public void Listen() { IPEndPoint iep = null; while (true) { string data = System.Text.Encoding.UTF8.GetString(udpClient.Receive(ref iep)); this.lbReceive.Items.Add(data); } } private void btnReceive_Click(object sender, EventArgs e) { thread = new Thread(new ThreadStart(Listen)); thread.IsBackground = true; thread.Start(); } private void formUdpReceive_Load(object sender, EventArgs e) { } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论