赞
踩
UDP客户端:
- Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- IPAddress iPAddress = new IPAddress(new byte[] { 192, 168, 1, 20 });
- IPEndPoint iPEndPoint = new IPEndPoint(iPAddress, 5000);
- byte[] data = Encoding.UTF8.GetBytes("客户端上线了");
- EndPoint EP = (EndPoint)iPEndPoint;
- udpClient.SendTo(data, EP);
UDP服务端:
- Socket udpServer = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
- ProtocolType.Udp);
- IPAddress ipAddress = new IPAddress(new byte[] { 192, 168, 1, 20 });
- IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 5000);
- udpServer.Bind(ipEndPoint);
- IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
- EndPoint EP= (EndPoint)ipep;
- byte[] data = new byte[1024];
- int length = udpServer.ReceiveFrom(data, ref EP);
- Console.WriteLine("接收到的数据:" + Encoding.UTF8.GetString(data, 0, length));
- udpServer.Close();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。