Export (0) Print
Expand All

UdpClient Class

Provides User Datagram Protocol (UDP) network services.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public class UdpClient : IDisposable
public class UdpClient implements IDisposable
public class UdpClient implements IDisposable

The UdpClient class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in one of the following two ways:

  • 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 then call the Connect method.

You can use any of the send methods provided in the UdpClient to send data to a remote device. Use the Receive method to receive data from remote hosts.

NoteNote

Do not call Send using a host name or IPEndPoint if you have already specified a default remote host. If you do, UdpClient will throw an exception.

UdpClient methods also allow you to send and receive multicast datagrams. Use the JoinMulticastGroup method to subscribe a UdpClient to a multicast group. Use the DropMulticastGroup method to unsubscribe a UdpClient from a multicast group.

The following example establishes a UdpClient connection using the host name www.contoso.com on port 11000. A small string message is sent to two separate remote host machines. The Receive method blocks execution until a message is received. Using the IPEndPoint passed to Receive, the identity of the responding host is revealed.

// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(11000);
    try{
         udpClient.Connect("www.contoso.com", 11000);

         // Sends a message to the host to which you have connected.
         Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
      
         udpClient.Send(sendBytes, sendBytes.Length);

         // Sends a message to a different host using optional hostname and port parameters.
         UdpClient udpClientB = new UdpClient();
         udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);

         //IPEndPoint object will allow us to read datagrams sent from any source.
         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

         // Blocks until a message returns on this socket from a remote host.
         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); 
         string returnData = Encoding.ASCII.GetString(receiveBytes);
   
         // Uses the IPEndPoint object to determine which of these two hosts responded.
         Console.WriteLine("This is the message you received " +
    	                              returnData.ToString());
         Console.WriteLine("This message was sent from " +
                                     RemoteIpEndPoint.Address.ToString() +
                                     " on their port number " +
                                     RemoteIpEndPoint.Port.ToString());

          udpClient.Close();
          udpClientB.Close();
          
          }  
       catch (Exception e ) {
                  Console.WriteLine(e.ToString());
        }

// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient();
try {
    udpClient.Connect("www.contoso.com", 11000);

    // Sends a message to the host to which you have connected.
    ubyte sendBytes[] = 
        Encoding.get_ASCII().GetBytes("Is anybody there?");
    udpClient.Send(sendBytes, sendBytes.length);

    // Sends a message to a different host using 
    // optional hostname and port parameters.
    UdpClient udpClientB = new UdpClient();
    udpClientB.Send(sendBytes, sendBytes.length, 
        "AlternateHostMachineName", 11000);

    // IPEndPoint object will allow us to read 
    // datagrams sent from any source.
    IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

    // Blocks until a message returns on this socket
    // from a remote host.
    ubyte receiveBytes[] = udpClient.Receive(remoteIpEndPoint);
    String returnData = Encoding.get_ASCII().GetString(receiveBytes);

    // Uses the IPEndPoint object to determine which of 
    // these two hosts responded.
    Console.WriteLine(("This is the message you received "
        + returnData.ToString()));
    Console.WriteLine(("This message was sent from " 
        + remoteIpEndPoint.get_Address().ToString() 
        + " on their port number " 
        + System.Convert.ToString(remoteIpEndPoint.get_Port())));
    udpClient.Close();
    udpClientB.Close();
}
catch (System.Exception e) {
    Console.WriteLine(e.ToString());
}

  • SocketPermission  To establish an outgoing connection or accept an incoming request.

System.Object
  System.Net.Sockets.UdpClient

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft