SoFunction
Updated on 2025-03-08

C# Example of using UdpClient class for simple communication

The UdpClient class provides some simple methods for sending and receiving connectionless UDP datagrams in blocked synchronization mode. Because UDP is a connectionless transmission protocol, it is not necessary to establish a remote host connection before sending and receiving data. But you can choose to use one of the following two methods to create a default remote host:

  • Create an instance of the UdpClient class using the remote host name and port number as parameters.
  • Create an instance of the UdpClient class and call the Connect method.

Data can be sent to a remote device using any of the sending methods provided in UdpClient. Use the Receive method to receive data from a remote host.

The UdpClient method also allows the sending and receiving of multicast datagrams. Use the JoinMulticastGroup method to place a UdpClient to a multicast group. Use the DropMulticastGroup method to cancel a subscription to a UdpClient from a multicast group.

/// <summary>
/// Client/// </summary>
class UDPSender
{
  static void Main(string[] args)
  {
    //Create a UdpClient object, 0 means that the system automatically allocates the sending port    //(If the server and client are running on the machine at the same time, the server needs to use different ports for receiving and sending the client, otherwise the two programs will cause a conflict if they use the same port)    UdpClient udpSender = new UdpClient(0);
    //Connect to the server and specify the receiving port    ("localhost", 11000);
    //Connect to the subnet broadcast address and specify the receiving port    //("192.168.1.255", 11000);
    //(In a network using the TCP/IP protocol, the IP address with all host identification segments of 1 is the broadcast address, and the broadcast address is transmitted to all computers involved in the host identification segment.    //For example, for the 192.168.1.0 (255.255.255.0) network segment, its broadcast address is 192.168.1.255 (255's binary is 111111111),    //When the issuance destination address is 192.168.1.255, it will be distributed to all computers on the network segment.  )    //Convert the message into a byte stream and send it to the server    byte[] sendBytes = ("Is anybody there?");
    (sendBytes, );
    //Close the link    ();
  }
}
/// <summary>
/// Server/// </summary>
class UDPReceive
{
  static void Main(string[] args)
  {
    //Create a UdpClient object, 11000 is the receiving port    UdpClient udpReceive = new UdpClient(11000);
    //Set the remote host, (, 0) represents receiving data sent by all IPs and all ports    IPEndPoint remoteIpEndPoint = new IPEndPoint(, 0);//or IPEndPoint remoteIpEndPoint = null;    // Listen to the data, after receiving the data, convert the data into a string and output it    byte[] receiveBytes = (ref remoteIpEndPoint);
    string returnData = (receiveBytes);
    ("This is the message you received " + ());
    ("This message was sent from " + () + " on their port number " + ()); 
    //Close the connection    ();
  }
}

Note: You need to run the server first and then run the client. Otherwise, the client has sent data before the server runs, and the server will not receive data.

The following is a simple example of using the UdpClient class for multicast groups. Adding the same broadcast group address can achieve multicasting. The range of multicast addresses ranges from 224.0.0.0 to 239.255.255.255.255.255. Multicast can be achieved using the same broadcast address.

/// <summary>
/// Multi-channel broadcast group client/// </summary>
class MulticastGroupClient
{
  static void Main(string[] args)
  {
    //Create a UdpClient object, 0 means that the system automatically allocates the sending port    var client = new UdpClient(0);
    //Add the broadcast address to the multicast group, the lifetime (router hop count) is 10    var ip = ("234.56.78.90");
    (ip, 10);
    //Define the endpoint (server IP and reception port), convert the message into a byte stream and send it to the server    var multicast = new IPEndPoint(ip, 7788);
    byte[] bytes = ("Hello from multicast.");
    (bytes, , multicast);
  }
}
/// <summary>
/// Multi-channel broadcast group server/// </summary>
class MulticastGroupServer
{
  static void Main(string[] args)
  {
    //Create a UdpClient object, 7788 is the receiving port    var client = new UdpClient(7788);
    //Add the broadcast address to the multicast group, the lifetime (router hop count) is 10    var ip = ("234.56.78.90");
    (ip, 10);
    //Set the remote host, (, 0) represents receiving data sent by all IPs and all ports    var multicast = new IPEndPoint(, 0);//or IPEndPoint multicast = null;    // Listen to the data, after receiving the data, convert the data into a string and output it    byte[] bytes = (ref multicast);
    string msg = (bytes);
    (msg);
  }
}

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!