static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.Encoding.Default.GetBytes("this message from client"); UdpClient client = new UdpClient(); client.Connect("127.0.0.1", 8989); client.Send(buffer, buffer.Length); client.Close(); }
static void Main(string[] args) { Console.WriteLine("接收端"); UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Any, 8989)); var iep=new IPEndPoint(IPAddress.Any, 8989); byte[] bytes = client.Receive(ref iep); string result= System.Text.Encoding.Default.GetString(bytes); client.Close(); Console.WriteLine(result); Console.ReadKey(); }