System.Net.Sockets.UdpClient.Send Method

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

Syntax

public int Send (byte[] dgram, int bytes, System.Net.IPEndPoint endPoint)

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.
endPoint
An System.Net.IPEndPoint that represents the host and port to which to send the datagram.

Returns

The number of bytes sent.

Remarks

The UdpClient.Send(Byte[], int, System.Net.IPEndPoint) method sends datagrams to the specified endpoint and returns the number of bytes successfully sent. Before calling this overload, you must first create an System.Net.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, 255.255.255.255, by specifying SocketOptionName.Broadcast for the System.Net.IPAddress.Address property of the System.Net.IPEndPoint. After you have created this System.Net.IPEndPoint, pass it to the UdpClient.Send(Byte[], int, System.Net.IPEndPoint) method as the endPoint parameter.

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

Note:

Do not provide an endPoint parameter to this method if you have already established a remote host with the UdpClient.Connect(string, int) method. If you do, the UdpClient.Send(Byte[], int, System.Net.IPEndPoint) method will throw a System.Net.Sockets.SocketException. If you receive a System.Net.Sockets.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.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0