日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

通过UDP的组播方式收发数据

發布時間:2025/6/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过UDP的组播方式收发数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

客戶端代碼

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.Text;namespace UDPTest {public partial class Form1 : Form{private UdpClient udpSend;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){udpSend = new UdpClient();udpSend.EnableBroadcast = true;//是否可以發送和接收廣播IPEndPoint iep = new IPEndPoint(IPAddress.Parse("224.100.0.10"), 8001);byte[] data = Encoding.UTF8.GetBytes(richTextBox1.Text);udpSend.Send(data, data.Length, iep);richTextBox1.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; using System.Net.Sockets;namespace UDPTest2 {public partial class Form1 : Form{UdpClient udpReceive;public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){}private void Form1_Load(object sender, EventArgs e){byte[] data = null;udpReceive = new UdpClient(8001);udpReceive.JoinMulticastGroup(IPAddress.Parse("224.100.0.10"), 50);//添加到多路廣播組,50為路由器跳數IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);while (true){data = udpReceive.Receive(ref iep);string str = Encoding.UTF8.GetString(data, 0, data.Length);MessageBox.Show(iep.ToString() + ":" + str);}}} }

?

總結

以上是生活随笔為你收集整理的通过UDP的组播方式收发数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。