首先要使用以下兩個命名空間
using System.Net; using System.Net.Sockets;
發送端程式碼如下:
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("192.168.1.255"), 1688); //可自行定義廣播區域跟Port
Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //定義發送的格式及有效區域
Server.EnableBroadcast = true;
Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
byte[] pushdata = new byte[1024]; //定義要送出的封包大小
while (true)
{
pushdata = Encoding.UTF8.GetBytes("asdasd"); //把要送出的資料轉成byte型態
Server.SendTo(pushdata, remoteIP); //送出的資料跟目的
Thread.Sleep(1000); //每秒發送一次
}
接收端程式碼如下:
IPEndPoint IPEnd = new IPEndPoint(IPAddress.Any, 1688);
Socket Client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Client.Bind(IPEnd);
EndPoint IP = (EndPoint)IPEnd; //我真的不知道為何一定要這行才能成功= =,誰能解釋一下
byte[] getdata = new byte[1024]; //要接收的封包大小
string input;
int recv;
while(true)
{
recv = Client.ReceiveFrom(getdata, ref IP); //把接收的封包放進getdata且傳回大小存入recv
input = Encoding.UTF8.GetString(getdata, 0, recv); //把接收的byte資料轉回string型態
Console.WriteLine("received: {0} from: {1}", input, IP.ToString());
}
文章標籤
全站熱搜
