This documentation is archived and is not being maintained.

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

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

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

public int Send (
	byte[] dgram,
	int bytes,
	string hostname,
	int port
)
public int Send (
	byte[] dgram, 
	int bytes, 
	String hostname, 
	int port
)
public function Send (
	dgram : byte[], 
	bytes : int, 
	hostname : String, 
	port : int
) : int
Not applicable.

Parameters

dgram

An array of type Byte that specifies 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 send the datagram.

port

The remote port number with which you intend to communicate.

Return Value

The number of bytes sent.

Exception typeCondition

ArgumentNullException

dgram is a null reference (Nothing in Visual Basic).

InvalidOperationException

The UdpClient has already established a default remote host.

ObjectDisposedException

The UdpClient is closed.

SocketException

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

The Send method sends datagrams to the values specified by the hostname and port parameters and returns the number of bytes successfully sent. You can send datagrams to the default broadcast address by specifying "255.255.255.255" for the hostname parameter value.

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.

NoteNote:

Do not provide a host name or port number 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 a host name and a 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());    
}

    UdpClient udpClient = new UdpClient();
    ubyte sendBytes[] = 
        Encoding.get_ASCII().GetBytes("Is anybody there");
    try {
        udpClient.Send(sendBytes, sendBytes.length,
            "www.contoso.com", 11000);
    }
    catch (System.Exception e) {
        Console.WriteLine(e.ToString());
    }
}

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Show: