System.Net.Sockets.UdpClient Class

Assembly: System.dll
Namespace: System.Net.Sockets
Summary
Provides User Datagram Protocol (UDP) network services.
C# Syntax:
public class UdpClient : IDisposable
Remarks
The UdpClient class builds upon the Socket class to provide UDP services at a higher level of abstraction. 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 using one of the following methods:

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



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

UdpClient methods allow you to also receive multicasted datagrams. Use UdpClient.JoinMulticastGroup and UdpClient.DropMulticastGroup to associate and disassociate a UdpClient with a multicast group.

Example
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 UdpClient.Receive method blocks execution until a message is received. Using the IPEndPoint passed to UdpClient.Receive, the identity of the responding host is revealed.
// 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.
         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());
        }

    
See also:
System.Net.Sockets Namespace | TcpClient | MSDN: tcpudp

System.Net.Sockets.UdpClient Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the UdpClient class.
ctor #2 Overloaded:
.ctor(int port)

Initializes a new instance of the UdpClient class that communicates on a specified port.
ctor #3 Overloaded:
.ctor(IPEndPoint localEP)

Initializes a new instance of the UdpClient class that communicates on a specified local endpoint.
ctor #4 Overloaded:
.ctor(string hostname, int port)

Initializes a new instance of the UdpClient class and connects to a specified remote host on a specified port.
Public Methods
Close Closes the UDP connection.
Connect Overloaded:
Connect(IPEndPoint endPoint)

Connects the client to a remote UDP host using the specified remote network endpoint.
Connect Overloaded:
Connect(IPAddress addr, int port)

Connects the client to a remote UDP host using the specified IP Address and port number.
Connect Overloaded:
Connect(string hostname, int port)

Establishes a connection to the specified port on the specified remote host.
DropMulticastGroup Leaves a multicast group.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
JoinMulticastGroup Overloaded:
JoinMulticastGroup(IPAddress multicastAddr)

Adds a UdpClient to a multicast group.
JoinMulticastGroup Overloaded:
JoinMulticastGroup(IPAddress multicastAddr, int timeToLive)

Adds a UdpClient to a multicast group with the specified Time to Live (TTL).
Receive Returns a UDP datagram that was sent by a remote host.
Send Overloaded:
Send(byte[] dgram, int bytes)

Sends a UDP datagram to a remote host.
Send Overloaded:
Send(byte[] dgram, int bytes, IPEndPoint endPoint)

Sends a UDP datagram to the host at the remote endpoint.
Send Overloaded:
Send(byte[] dgram, int bytes, string hostname, int port)

Sends a UDP datagram to a specified port on a specified remote host.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Properties
Active Read-write

Gets or sets a value indicating whether a connection to a remote host has been made.
Client Read-write

Gets or sets the underlying network socket.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Net.Sockets.UdpClient Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the UdpClient class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public UdpClient();
Remarks
The default constructor initializes a new UdpClient. If you want to establish a default remote host prior to sending and receiving data, you must call the UdpClient.Connect method.

Note If you establish a default remote host using UdpClient.Connect, you do not have to specify the remote host when calling UdpClient.Send.
Example
The following example demonstrates how to use the default constructor to create an instance of the UdpClient class.
           //Creates an instance of the UdpClient class using the default constructor.
           UdpClient udpClient = new UdpClient();

    

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the UdpClient class that communicates on a specified port.
C# Syntax:
public UdpClient(
   int port
);
Parameters:

port

The local port number from which you intend to communicate.

Exceptions
Exception Type Condition
ArgumentException The port parameter is greater than IPEndPoint.MaxPort or less than IPEndPoint.MinPort.
Remarks
The port parameter specifies the local port number on which you intend to communicate. This constructor creates an underlying Socket and binds it to port. This constructor allows the network system to assign the appropriate local network interface.
Example
The following example demonstrates using a local port number to create an instance of the UdpClient class.
           //Creates an instance of the UdpClient class to listen on
           // the default interface using a particular port.
           try{
                    UdpClient udpClient = new UdpClient(11000);
           }  
           catch (Exception e ) {
                     Console.WriteLine(e.ToString());
             }

    

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the UdpClient class that communicates on a specified local endpoint.
C# Syntax:
public UdpClient(
   IPEndPoint localEP
);
Parameters:

localEP

The local endpoint to which you bind the UDP connection.

Exceptions
Exception Type Condition
ArgumentNullException The localEP parameter is null.
Remarks
The localEP parameter specifies the local IPEndPoint. This constructor creates an underlying socket and binds that socket to localEp. Call the UdpClient.Connect method if you want to establish a default remote host prior to sending and receiving data.

Note Use this constructor to specify which local network interface (IP address), and port number to use for sending and receiving data.
Example
The following example demonstrates how to create an instance of the UdpClient class using a local endpoint.

           //Creates an instance of the UdpClient class using a local endpoint.
            IPAddress ipAddress = Dns.Resolve(Dns.GetHostName()).AddressList[0];
            IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 11000);
           
           try{
                UdpClient udpClient = new UdpClient(ipLocalEndPoint);
           }
           catch (Exception e ) {
                      Console.WriteLine(e.ToString());
           }

    
See also:
IPEndPoint

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the UdpClient class and connects to a specified remote host on a specified port.
C# Syntax:
public UdpClient(
   string hostname,
   int port
);
Parameters:

hostname

The name of the remote DNS host to which you intend to connect.

port

The remote port number to which you intend to connect.

Exceptions
Exception Type Condition
ArgumentNullException The hostname parameter is null.
ArgumentException The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort.
SocketException An error occurred while connecting to the remote host.
Remarks
This constructor initializes a new UdpClient and establishes a remote host connection by calling UdpClient.Connect using the provided hostname and port parameter. This constructor allows you to initialize and connect in one convenient step.
Example
The following example demonstrates how to create an instance of the UdpClient class using a host name and port number.

           //Creates an instance of the UdpClient class with a remote host name and a port number.
           try{
                UdpClient udpClient = new UdpClient("www.contoso.com",11000);
           }
           catch (Exception e ) {
                      Console.WriteLine(e.ToString());
           }

    
See also:
TcpClient.#ctor

Return to top


Property: Active (read-write)
Summary
Gets or sets a value indicating whether a connection to a remote host has been made.
C# Syntax:
protected bool Active {get; set;}
Remarks
Classes deriving from UdpClient can use this property to determine if a default remote host has been established. A default remote host is created by calling UdpClient.Connect.
Example
The following example demonstrates a derived class using the protected property UdpClient.Active. In this example, MyUdpClientDerivedClass verifies that the connection is active before obtaining the underlying Socket.
// This derived class demonstrate the use of three protected methods belonging to the UdpClient class.
public class MyUdpClientDerivedClass : UdpClient{

public MyUdpClientDerivedClass() : base(){
}
public void UsingProtectedMethods(){

  //Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established.
  if (this.Active){
      //Calls the protected Client property belonging to the UdpClient base class.
      Socket s = this.Client;
      //Uses the Socket returned by Client to set an option that is not available using UdpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  }
  
}

}

    

Return to top


Property: Client (read-write)
Summary
Gets or sets the underlying network socket.
C# Syntax:
protected Socket Client {get; set;}
Remarks
UdpClient creates a Socket to send and receive data over a network. Classes deriving from UdpClient can use this property to get or set this Socket. Use the underlying Socket returned from UdpClient.Client if you require access beyond that which UdpClient provides. You can also use UdpClient.Client to set the underlying Socket to an existing Socket. This might be useful if you want take advantage of the simplicity of UdpClient using a pre existing Socket.
Example
The following example demonstrates a derived class using the protected property UdpClient.Client. In this example, MyUdpClientDerivedClass obtains the underlying Socket to enable broadcasting.
// This derived class demonstrate the use of three protected methods belonging to the UdpClient class.
public class MyUdpClientDerivedClass : UdpClient{

public MyUdpClientDerivedClass() : base(){
}
public void UsingProtectedMethods(){

  //Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established.
  if (this.Active){
      //Calls the protected Client property belonging to the UdpClient base class.
      Socket s = this.Client;
      //Uses the Socket returned by Client to set an option that is not available using UdpClient.
      s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  }
  
}

}

    
See also:
Socket

Return to top


Method: Close()
Summary
Closes the UDP connection.
C# Syntax:
public void Close();
Exceptions
Exception Type Condition
SocketException An error occurred while closing the socket.
Remarks
TcpClient.Close disposes of the UDP connection.
Example
The following example demonstrates closing UdpClient by calling UdpClient.Close.
        // Closes the UDP client by calling the public method Close().
        udpClient.Close();

    
See also:
Socket.Close

Return to top


Overloaded Method: Connect(
   IPEndPoint endPoint
)
Summary
Connects the client to a remote UDP host using the specified remote network endpoint.
C# Syntax:
public void Connect(
   IPEndPoint endPoint
);
Parameters:

endPoint

The network endpoint to which you intend to connect.

Exceptions
Exception Type Condition
SocketException An error occurred while connecting to the remote host.
Remarks
UdpClient.Connect establishes a UDP connection using the specified network endpoint. Before you call UdpClient.Connect, you must first create an instance of the IPEndPoint class using an IP address and a port number. This is your network endpoint. You can then call UdpClient.Connect and pass the IPEndPoint as its parameter. UdpClient.Connect then establishes a connection with the remote host by calling the Socket.Connect method of the underlying Socket. After establishing a connection, you can use the UdpClient.Send and UdpClient.Receive methods to communicate with the remote host.

Note To set the underlying Socket to broadcast mode, create the IPEndPoint using the IPAddress.Broadcast field.

Note Do not specify a remote hostname or IPEndPoint when calling UdpClient.Send. If you have already established a default remote host, UdpClient will throw an exception. If you want call UdpClient.Send using a hostname or IPEndPoint, you must call UdpClient.Connect and specify a different remote host, or close the existing UdpClient and use a separate instance of the UdpClient class.
Example
The following example uses an IPEndPoint to connect with a remote host.
   //Uses a remote endpoint to establish a socket connection.
   UdpClient udpClient = new UdpClient();
   IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
   IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);
   try{
   	udpClient.Connect(ipEndPoint);
   }
   catch (Exception e ) {
              Console.WriteLine(e.ToString());
          }

    
See also:
Socket | UdpClient.Send | UdpClient.Receive

Return to top


Overloaded Method: Connect(
   IPAddress addr,
   int port
)
Summary
Connects the client to a remote UDP host using the specified IP Address and port number.
C# Syntax:
public void Connect(
   IPAddress addr,
   int port
);
Parameters:

addr

The IP address of the host to which you intend to connect.

port

The port number to which you intend to connect.

Exceptions
Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
ArgumentNullException The address parameter is null.
ArgumentOutOfRangeException The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort.
SocketException An error occurred while connecting to the remote host.
Remarks
TcpClient.Connect establishes a UDP connection using the port and IP address specified. It does so by calling the Socket.Connect method of the underlying Socket. After establishing a connection, you can use the UdpClient.Send and UdpClient.Receive methods to communicate with the remote host.

Note To set the underlying Socket to broadcast mode, create the IPAddress using the IPAddress.Broadcast enumerated value.

Note Do not specify a remote hostname or IPEndPoint when calling UdpClient.Send. If you have already established a default remote host, UdpClient will throw an exception. If you want to call UdpClient.Send using a hostname or IPEndPoint, you must call UdpClient.Connect and specify a different remote host, or close the existing UdpClient and use a separate instance of the UdpClient class.
Example
The following example uses an IP Address and port number to connect with a remote host.
      //Uses the IP address and port number to establish a socket connection.
      UdpClient udpClient = new UdpClient();
      IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
      try{
          udpClient.Connect(ipAddress, 11003);
      }
      catch (Exception e ) {
                 Console.WriteLine(e.ToString());
      }

    
See also:
Socket | UdpClient.Send | UdpClient.Receive

Return to top


Overloaded Method: Connect(
   string hostname,
   int port
)
Summary
Establishes a connection to the specified port on the specified remote host.
C# Syntax:
public void Connect(
   string hostname,
   int port
);
Parameters:

hostname

DNS name of the remote host to which you intend to connect.

port

Port number on the remote host to which you intend to connect.

Exceptions
Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
ArgumentException The port parameter is not between IPEndPoint.MinPort and IPEndPoint.MaxPort.
SocketException An error occurred while connecting to the remote host.
Remarks
TcpClient.Connect establishes a UDP connection to the remote host using the specified port and hostname. This method establishes a connection by calling the Socket.Connect method of the underlying Socket. Once a connection is established, you can use the UdpClient.Send and UdpClient.Receive methods to communicate with the remote host.

Note Do not specify a remote hostname or IPEndPoint when calling UdpClient.Send. If you have already established a default remote host, UdpClient will throw an exception. If you want to call UdpClient.Send using a hostname or IPEndPoint, you must call UdpClient.Connect and specify a different remote host, or close the existing UdpClient and use a separate instance of the UdpClient class.
Example
The following example uses the host name and port number to connect to a remote host.
     //Uses a host name and port number to establish a socket connection.
    UdpClient udpClient = new UdpClient();
    try{
        udpClient.Connect("www.contoso.com", 11002);
    }
    catch (Exception e ) {
               Console.WriteLine(e.ToString());
           }

    
See also:
Socket | UdpClient.Send | UdpClient.Receive

Return to top


Method: DropMulticastGroup(
   IPAddress multicastAddr
)
Summary
Leaves a multicast group.
C# Syntax:
public void DropMulticastGroup(
   IPAddress multicastAddr
);
Parameters:

multicastAddr

The IPAddress of the multicast group to leave.

Exceptions
Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
SocketException The Socket was unable to leave the multicast group.
Remarks
UdpClient.DropMulticastGroup disassociates UdpClient from a multicast group. Calling UdpClient.DropMulticastGroup disables a UdpClient from receiving multicasted datagrams broadcasted to the specified IP address.
Example
The following example demonstrates how to drop a multicast group by providing a multicast address.
         // Informs the server that you want to remove yourself from the multicast client list.
         try{
             udpClient.DropMulticastGroup(multicastIpAddress);
         }
         catch ( Exception e ){
             Console.WriteLine( e.ToString());
         }

    
See also:
UdpClient.JoinMulticastGroup

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~UdpClient();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: JoinMulticastGroup(
   IPAddress multicastAddr
)
Summary
Adds a UdpClient to a multicast group.
C# Syntax:
public void JoinMulticastGroup(
   IPAddress multicastAddr
);
Parameters:

multicastAddr

The multicast IPAddress of the group you want to join.

Exceptions
Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
SocketException The socket was unable to join the multicast group.
Remarks
UdpClient.JoinMulticastGroup uses the specified IPAddress to associate the UdpClient with a multicast group. Calling UdpClient.JoinMulticastGroup enables a UdpClient to receive multicasted datagrams broadcasted to the specified IP address.
Example
The following example demonstrates how to join a multicast group by providing a multicast address.
           UdpClient udpClient = new UdpClient();
           IPAddress multicastIpAddress = Dns.Resolve("MulticastGroupName").AddressList[0];  
           try{
               udpClient.JoinMulticastGroup(multicastIpAddress);
           }
           catch ( Exception e ){
               Console.WriteLine( e.ToString());	
           }

    
See also:
UdpClient.DropMulticastGroup

Return to top


Overloaded Method: JoinMulticastGroup(
   IPAddress multicastAddr,
   int timeToLive
)
Summary
Adds a UdpClient to a multicast group with the specified Time to Live (TTL).
C# Syntax:
public void JoinMulticastGroup(
   IPAddress multicastAddr,
   int timeToLive
);
Parameters:

multicastAddr

The IPAddress of the multicast group to join.

timeToLive

The TTL measured in router hops.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The TTL provided is not between 0 and 255
ObjectDisposedException The underlying Socket has been closed.
SocketException The Socket was unable to join the multicast group.
Remarks
UdpClient.JoinMulticastGroup associates the UdpClient with a multicast group. Calling UdpClient.JoinMulticastGroup enables a UdpClient to receive multicasted datagrams broadcasted to the specified IP address. This overload requires two parameters:

Note If you want multicast packets to go beyond the local network, you must set TTL to a number higher than 1.
Example
The following example demonstrates how to join a multicast group by providing two parameters, a multicast address, and a number representing the TTL.
           UdpClient udpClient = new UdpClient();
           // Creates an IPAddress to use to join and drop the multicast group.
           IPAddress multicastIpAddress = IPAddress.Parse("239.255.255.255");
           
           try{
                // The packet dies after 50 router hops.
                udpClient.JoinMulticastGroup(multicastIpAddress, 50);
           }
           catch ( Exception e ){
               Console.WriteLine( e.ToString());
           }

    
See also:
UdpClient.DropMulticastGroup

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Receive(
   ref IPEndPoint remoteEP
)
Summary
Returns a UDP datagram that was sent by a remote host.
C# Syntax:
public byte[] Receive(
   ref IPEndPoint remoteEP
);
Parameters:

remoteEP

An IPEndPoint representing the remote host from which the data was sent.

Return Value:
Datagram data as an array of bytes.
Exceptions
Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
SocketException An error occurred while reading the Socket.
Remarks
UdpClient.Receive polls the underlying Socket for incoming datagrams. When datagrams arrive, UdpClient.Receive returns the data portion of the datagram as a byte array. UdpClient.Receive populates the remoteEP parameter with the IPAddress and port number of the sender.
Example
The following example demonstrates UdpClient.Receive. The UdpClient.Receive method blocks execution until it receives a message. Using the IPEndPoint passed to UdpClient.Receive, the identity of the responding host is revealed.
   //Creates a UdpClient for reading incoming data.
   UdpClient receivingUdpClient = new UdpClient();

   //Creates an IPEndPoint to record the IP Address and port number of the sender. 
  // The IPEndPoint will allow you to read datagrams sent from any source.
   IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
   try{

       // Blocks until a message returns on this socket from a remote host.
       Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); 

       string returnData = Encoding.ASCII.GetString(receiveBytes);
   
       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());
   }
   catch ( Exception e ){
       Console.WriteLine(e.ToString()); 
   }
   

    
See also:
UdpClient.Send | IPEndPoint

Return to top


Overloaded Method: Send(
   byte[] dgram,
   int bytes
)
Summary
Sends a UDP datagram to a remote host.
C# Syntax:
public int Send(
   byte[] dgram,
   int bytes
);
Parameters:

dgram

The UDP datagram that you intend to send represented as an array of bytes.

bytes

The number of bytes in the datagram.

Return Value:
The number of bytes sent.
Exceptions
Exception Type Condition
ArgumentException The dgram parameter is null.
InvalidOperationException UdpClient is already connected to a remote host.
ObjectDisposedException The underlying Socket has been closed.
SocketException An error occurred while sending data to the Socket.
Remarks
Before using UdpClient.Send, you must first use UdpClient.Connect to establish a remote host connection. UdpClient sends the data by calling the Socket.Send method of the underlying Socket.
Example
The following example demonstrates UdpClient.Send. You must establish a connection with the remote host prior to using this overload.
       UdpClient udpClient = new UdpClient("www.contoso.com", 11000);
       Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
       try{
           udpClient.Send(sendBytes, sendBytes.Length);
       }
       catch ( Exception e ){
           Console.WriteLine( e.ToString());
       }

    
See also:
UdpClient.Connect | MSDN: encodingbasetypes

Return to top


Overloaded Method: Send(
   byte[] dgram,
   int bytes,
   IPEndPoint endPoint
)
Summary
Sends a UDP datagram to the host at the remote endpoint.
C# Syntax:
public int Send(
   byte[] dgram,
   int bytes,
   IPEndPoint endPoint
);
Parameters:

dgram

The UDP datagram that you intend to send represented as an array of bytes.

bytes

The number of bytes in the datagram.

endPoint

An IPEndPoint that represents the host and port to which to send the datagram.

Return Value:
The number of bytes sent.
Exceptions
Exception Type Condition
ArgumentException The dgram parameter is null.
InvalidOperationException UdpClient is already connected to a remote host.
ObjectDisposedException The underlying Socket has been closed.
SocketException An error occurred while sending data to the Socket.
Remarks
UdpClient.Send establishes a UDP connection to the specified IPEndPoint and then sends the datagram. The data is sent by calling the Socket.SendTo method of the underlying Socket.

Note To send the datagram as a broadcast message, create the IPEndPoint using the IPAddress.Broadcast enumerated value.

Note If you UdpClient.Connect to a remote host before calling this method, UdpClient will throw an exception. If you have already established a default remote host using UdpClient.Connect, consider one of the following before using UdpClient.Send:
Example
The following example demonstrates UdpClient.Send. This example uses an IPEndPoint to specify the target host.
       UdpClient udpClient = new UdpClient();
       IPAddress ipAddress = Dns.Resolve("www.contoso.com").AddressList[0];
       IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);	
       
       Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
       try{
           udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
       }
       catch ( Exception e ){
           Console.WriteLine(e.ToString());	
       }

    
See also:
MSDN: encodingbasetypes

Return to top


Overloaded Method: Send(
   byte[] dgram,
   int bytes,
   string hostname,
   int port
)
Summary
Sends a UDP datagram to a specified port on a specified remote host.
C# Syntax:
public int Send(
   byte[] dgram,
   int bytes,
   string hostname,
   int port
);
Parameters:

dgram

The UDP datagram that you intend to send represented as an array of bytes.

bytes

The number of bytes in the datagram.

hostname

The name of the remote host to which you intend to connect.

port

The remote port number with which you intend to communicate.

Return Value:
The number of bytes sent.
Exceptions
Exception Type Condition
ArgumentException The dgram parameter is null.
InvalidOperationException UdpClient is already connected to a remote host.
ObjectDisposedException The underlying Socket has been closed.
SocketException An error occurred while sending data to the Socket.
Remarks
UdpClient.Send accomplishes the following:

If you UdpClient.Connect to a remote host before calling this method, UdpClient will throw an exception. If you have already established a default remote host using UdpClient.Connect, consider one of the following before using UdpClient.Send:

Example
The following example demonstrates UdpClient.Send. This example uses a host name and port number to identify the target host.
       UdpClient udpClient = new UdpClient();

       Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
       try{
           udpClient.Send(sendBytes, sendBytes.Length, "www.contoso.com", 11000);
       }
       catch ( Exception e ){
           Console.WriteLine(e.ToString());	
       }

    
See also:
MSDN: encodingbasetypes

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.