using System.Net; using System.Net.Sockets; UdpClient client = new UdpClient(); byte[] data = Encoding.UTF8.GetBytes("Hello, World!"); client.Send(data, data.Length, "192.168.1.100", 1234);
using System.Net; using System.Net.Sockets; UdpClient client = new UdpClient(1234); IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0); byte[] data = client.Receive(ref remoteEP); string message = Encoding.UTF8.GetString(data); Console.WriteLine($"Received message: {message} from {remoteEP.ToString()}");In this example, a new UdpClient object is created and bound to port 1234. The client receives a UDP datagram from any remote host and prints out the message and the IP address and port of the remote host. Package library: System.Net.Sockets