UdpClient.Send Method (Byte(), Int32, String, Int32)
Sends a UDP datagram to a specified port on a specified remote host.
Assembly: System (in System.dll)
'Declaration Public Function Send ( _ dgram As Byte(), _ bytes As Integer, _ hostname As String, _ port As Integer _ ) As Integer
- 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.String
The name of the remote host to which you intend to send the datagram.
- Type: System.Int32
The remote port number with which you intend to communicate.
Return ValueType: System.Int32
The number of bytes sent.
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.
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.
Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.