UdpClient UdpClient UdpClient UdpClient Class

Definition

Provides User Datagram Protocol (UDP) network services.

public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
    interface IDisposable
Public Class UdpClient
Implements IDisposable
Inheritance
UdpClientUdpClientUdpClientUdpClient
Implements

Examples

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.

// With this constructor the local port number is arbitrarily assigned.
UdpClient^ udpClient = gcnew UdpClient;
try
{
   udpClient->Connect( "host.contoso.com", 11000 );

   // Send message to the host to which you have connected.
   array<Byte>^sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
   udpClient->Send( sendBytes, sendBytes->Length );

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

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

   // Block until a message returns on this socket from a remote host.
   array<Byte>^receiveBytes = udpClient->Receive( RemoteIpEndPoint );
   String^ returnData = Encoding::ASCII->GetString( receiveBytes );

   // Use the IPEndPoint object to determine which of these two hosts responded.
   Console::WriteLine( String::Concat( "This is the message you received ", returnData->ToString() ) );
   Console::WriteLine( String::Concat( "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(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.
   Dim udpClient As New UdpClient(11000)
   Try
      udpClient.Connect("www.contoso.com", 11000)
      
      ' Sends a message to the host to which you have connected.
      Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
      
      udpClient.Send(sendBytes, sendBytes.Length)
      
      ' Sends message to a different host using optional hostname and port parameters.
      Dim udpClientB As New UdpClient()
      udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
      
      ' IPEndPoint object will allow us to read datagrams sent from any source.
      Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
      
      ' UdpClient.Receive blocks until a message is received from a remote host.
      Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
      Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
      
      ' Which one 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 e As Exception
      Console.WriteLine(e.ToString())
   End Try
End Sub 

Remarks

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.

Note

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.

Constructors

UdpClient() UdpClient() UdpClient() UdpClient()

Initializes a new instance of the UdpClient class.

UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily) UdpClient(AddressFamily)

Initializes a new instance of the UdpClient class.

UdpClient(Int32) UdpClient(Int32) UdpClient(Int32) UdpClient(Int32)

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily) UdpClient(Int32, AddressFamily)

Initializes a new instance of the UdpClient class and binds it to the local port number provided.

UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint) UdpClient(IPEndPoint)

Initializes a new instance of the UdpClient class and binds it to the specified local endpoint.

UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32) UdpClient(String, Int32)

Initializes a new instance of the UdpClient class and establishes a default remote host.

Properties

Active Active Active Active

Gets or sets a value indicating whether a default remote host has been established.

Available Available Available Available

Gets the amount of data received from the network that is available to read.

Client Client Client Client

Gets or sets the underlying network Socket.

DontFragment DontFragment DontFragment DontFragment

Gets or sets a Boolean value that specifies whether the UdpClient allows Internet Protocol (IP) datagrams to be fragmented.

EnableBroadcast EnableBroadcast EnableBroadcast EnableBroadcast

Gets or sets a Boolean value that specifies whether the UdpClient may send or receive broadcast packets.

ExclusiveAddressUse ExclusiveAddressUse ExclusiveAddressUse ExclusiveAddressUse

Gets or sets a Boolean value that specifies whether the UdpClient allows only one client to use a port.

MulticastLoopback MulticastLoopback MulticastLoopback MulticastLoopback

Gets or sets a Boolean value that specifies whether outgoing multicast packets are delivered to the sending application.

Ttl Ttl Ttl Ttl

Gets or sets a value that specifies the Time to Live (TTL) value of Internet Protocol (IP) packets sent by the UdpClient.

Methods

AllowNatTraversal(Boolean) AllowNatTraversal(Boolean) AllowNatTraversal(Boolean) AllowNatTraversal(Boolean)

Enables or disables Network Address Translation (NAT) traversal on a UdpClient instance.

BeginReceive(AsyncCallback, Object) BeginReceive(AsyncCallback, Object) BeginReceive(AsyncCallback, Object) BeginReceive(AsyncCallback, Object)

Receives a datagram from a remote host asynchronously.

BeginSend(Byte[], Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, AsyncCallback, Object)

Sends a datagram to a remote host asynchronously. The destination was specified previously by a call to Connect.

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object) BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object) BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object) BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

Sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object) BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

Sends a datagram to a destination asynchronously. The destination is specified by the host name and port number.

Close() Close() Close() Close()

Closes the UDP connection.

Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32) Connect(IPAddress, Int32)

Establishes a default remote host using the specified IP address and port number.

Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint) Connect(IPEndPoint)

Establishes a default remote host using the specified network endpoint.

Connect(String, Int32) Connect(String, Int32) Connect(String, Int32) Connect(String, Int32)

Establishes a default remote host using the specified host name and port number.

Dispose() Dispose() Dispose() Dispose()

Releases the managed and unmanaged resources used by the UdpClient.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Releases the unmanaged resources used by the UdpClient and optionally releases the managed resources.

DropMulticastGroup(IPAddress) DropMulticastGroup(IPAddress) DropMulticastGroup(IPAddress) DropMulticastGroup(IPAddress)

Leaves a multicast group.

DropMulticastGroup(IPAddress, Int32) DropMulticastGroup(IPAddress, Int32) DropMulticastGroup(IPAddress, Int32) DropMulticastGroup(IPAddress, Int32)

Leaves a multicast group.

EndReceive(IAsyncResult, IPEndPoint) EndReceive(IAsyncResult, IPEndPoint) EndReceive(IAsyncResult, IPEndPoint) EndReceive(IAsyncResult, IPEndPoint)

Ends a pending asynchronous receive.

EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult) EndSend(IAsyncResult)

Ends a pending asynchronous send.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize() Finalize() Finalize() Finalize()
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
JoinMulticastGroup(Int32, IPAddress) JoinMulticastGroup(Int32, IPAddress) JoinMulticastGroup(Int32, IPAddress) JoinMulticastGroup(Int32, IPAddress)

Adds a UdpClient to a multicast group.

JoinMulticastGroup(IPAddress) JoinMulticastGroup(IPAddress) JoinMulticastGroup(IPAddress) JoinMulticastGroup(IPAddress)

Adds a UdpClient to a multicast group.

JoinMulticastGroup(IPAddress, Int32) JoinMulticastGroup(IPAddress, Int32) JoinMulticastGroup(IPAddress, Int32) JoinMulticastGroup(IPAddress, Int32)

Adds a UdpClient to a multicast group with the specified Time to Live (TTL).

JoinMulticastGroup(IPAddress, IPAddress) JoinMulticastGroup(IPAddress, IPAddress) JoinMulticastGroup(IPAddress, IPAddress) JoinMulticastGroup(IPAddress, IPAddress)

Adds a UdpClient to a multicast group.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Receive(IPEndPoint) Receive(IPEndPoint) Receive(IPEndPoint) Receive(IPEndPoint)

Returns a UDP datagram that was sent by a remote host.

ReceiveAsync() ReceiveAsync() ReceiveAsync() ReceiveAsync()

Returns a UDP datagram asynchronously that was sent by a remote host.

Send(Byte[], Int32) Send(Byte[], Int32) Send(Byte[], Int32) Send(Byte[], Int32)

Sends a UDP datagram to a remote host.

Send(Byte[], Int32, IPEndPoint) Send(Byte[], Int32, IPEndPoint) Send(Byte[], Int32, IPEndPoint) Send(Byte[], Int32, IPEndPoint)

Sends a UDP datagram to the host at the specified remote endpoint.

Send(Byte[], Int32, String, Int32) Send(Byte[], Int32, String, Int32) Send(Byte[], Int32, String, Int32) Send(Byte[], Int32, String, Int32)

Sends a UDP datagram to a specified port on a specified remote host.

SendAsync(Byte[], Int32) SendAsync(Byte[], Int32) SendAsync(Byte[], Int32) SendAsync(Byte[], Int32)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32, IPEndPoint) SendAsync(Byte[], Int32, IPEndPoint) SendAsync(Byte[], Int32, IPEndPoint) SendAsync(Byte[], Int32, IPEndPoint)

Sends a UDP datagram asynchronously to a remote host.

SendAsync(Byte[], Int32, String, Int32) SendAsync(Byte[], Int32, String, Int32) SendAsync(Byte[], Int32, String, Int32) SendAsync(Byte[], Int32, String, Int32)

Sends a UDP datagram asynchronously to a remote host.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose() IDisposable.Dispose()

Security

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

Applies to

See Also