1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
void Send(string msg) { UdpClient udpClient = new UdpClient(); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, 10249); byte[] buffer = Encoding.UTF8.GetBytes(msg); udpClient.Send(buffer, buffer.Length, ipEndPoint); }
static void Main() { UdpClient udpServer = new UdpClient(10249); IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, 10249);
udpServer.BeginReceive(Received, udpServer); }
void Received(IAsyncResult iar) { udpServer = iar.AsyncState as UdpClient; byte[] buffer = udpServer.EndReceive(iar, ref ipEndPoint);
string m = Encoding.UTF8.GetString(buffer).Trim();
Console.WriteLine("Receive:{0}",m);
udpServer.BeginReceive(Received, udpServer); }
|