UdpClient.Send Method (Byte[], Int32, IPEndPoint)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

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

public int Send(
	byte[] dgram,
	int bytes,
	IPEndPoint endPoint


Type: System.Byte[]

An array of type Byte that specifies the UDP datagram that you intend to send, represented as an array of bytes.

Type: System.Int32

The number of bytes in the datagram.

Type: System.Net.IPEndPoint

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

Return Value

Type: System.Int32

The number of bytes sent.

Exception Condition

dgram is null.


UdpClient has already established a default remote host.


UdpClient is closed.


An error occurred when accessing the socket. See the Remarks section for more information.

The Send method sends datagrams to the specified endpoint and returns the number of bytes successfully sent. Before calling this overload, you must first create an IPEndPoint using the IP address and port number of the remote host to which your datagrams will be delivered. You can send datagrams to the default broadcast address,, by specifying SocketOptionName.Broadcast for the Address property of the IPEndPoint. After you have created this IPEndPoint, pass it to the Send method as the endPoint parameter.

If you want to send datagrams to any other broadcast address, use the Client method to obtain the underlying Socket, and set the socket option to SocketOptionName.Broadcast. You can also revert to using the Socket class.


Do not provide an endPoint parameter to this method if you have already established a remote host with the Connect method. If you do, the Send method will throw a SocketException. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Sockets version 2 API error code documentation in MSDN for a detailed description of the error.

The following example demonstrates the Send method. 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?");
    udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
catch ( Exception e ){

.NET Framework
Available since 1.1
Return to top