UdpClient.Send Method (Byte[], Int32)
Sends a UDP datagram to a remote host.
Assembly: System (in System.dll)
Parameters
- dgram
- Type: System.Byte[]
An array of type Byte that specifies the UDP datagram that you intend to send represented as an array of bytes.
- bytes
- Type: System.Int32
The number of bytes in the datagram.
| Exception | Condition |
|---|---|
| ArgumentNullException |
dgram is null. |
| 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. |
This overload sends datagrams to the remote host established in the Connect method and returns the number of bytes sent. If you do not call Connect before calling this overload, 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.
If you want to send datagrams to a different remote host, you must call the Connect method and specify the desired remote host. Use either of the other Send method overloads to send datagrams to a broadcast address.
The following example demonstrates the Send method. You must establish a default 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()); }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.