Basic UDP Client

suggest change

This code example creates a UDP client then sends “Hello World” across the network to the intended recipient. A listener does not have to be active, as UDP Is connectionless and will broadcast the message regardless. Once the message is sent, the clients work is done.

byte[] data = Encoding.ASCII.GetBytes("Hello World");
string ipAddress = "";
string sendPort = 55600;
     using (var client = new UdpClient())
         IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), sendPort);
         client.Send(data, data.Length);
catch (Exception ex)

Below is an example of a UDP listener to complement the above client. It will constantly sit and listen for traffic on a given port and simply write that data to the console. This example contains a control flag ‘done’ that is not set internally and relies on something to set this to allow for ending the listener and exiting.

bool done = false;
int listenPort = 55600;
using(UdpClinet listener = new UdpClient(listenPort))
    IPEndPoint listenEndPoint = new IPEndPoint(IPAddress.Any, listenPort);
        byte[] receivedData = listener.Receive(ref listenPort);

        Console.WriteLine("Received broadcast message from client {0}", listenEndPoint.ToString());

        Console.WriteLine("Decoded data is:");
        Console.WriteLine(Encoding.ASCII.GetString(receivedData)); //should be "Hello World" sent from above client

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents