.NET Framework Class Library
UdpClient.Send Method (Byte[], Int32, IPEndPoint)

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

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

Syntax

Visual Basic (Declaration)
Public Function Send ( _
    dgram As Byte(), _
    bytes As Integer, _
    endPoint As IPEndPoint _
) As Integer
Visual Basic (Usage)
Dim instance As UdpClient
Dim dgram As Byte()
Dim bytes As Integer
Dim endPoint As IPEndPoint
Dim returnValue As Integer

returnValue = instance.Send(dgram, bytes, endPoint)
C#
public int Send (
    byte[] dgram,
    int bytes,
    IPEndPoint endPoint
)
C++
public:
int Send (
    array<unsigned char>^ dgram, 
    int bytes, 
    IPEndPoint^ endPoint
)
J#
public int Send (
    byte[] dgram, 
    int bytes, 
    IPEndPoint endPoint
)
JScript
public function Send (
    dgram : byte[], 
    bytes : int, 
    endPoint : IPEndPoint
) : int

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

Return Value

The number of bytes sent.
Exceptions

Exception typeCondition

ArgumentNullException

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

InvalidOperationException

UdpClient has already established a default remote host.

ObjectDisposedException

UdpClient is closed.

SocketException

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

Remarks

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, 255.255.255.255, 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.

NoteNote

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.

Example

The following example demonstrates the Send method. This example uses an IPEndPoint to specify the target host.

Visual Basic
Dim udpClient As New UdpClient()
Dim ipAddress As IPAddress = Dns.Resolve("www.contoso.com").AddressList(0)
Dim ipEndPoint As New IPEndPoint(ipAddress, 11004)

Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
Try
   udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint)
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try
C#
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?");
try{
    udpClient.Send(sendBytes, sendBytes.Length, ipEndPoint);
}
catch ( Exception e ){
    Console.WriteLine(e.ToString());    
}
C++
UdpClient^ udpClient = gcnew UdpClient;
IPAddress^ ipAddress = Dns::Resolve( "www.contoso.com" )->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddress,11004 );

array<Byte>^ sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
try
{
   udpClient->Send( sendBytes, sendBytes->Length, ipEndPoint );
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
J#
    UdpClient udpClient = new UdpClient();
    IPAddress ipAddress = 
        Dns.Resolve("www.contoso.com").get_AddressList()[0];
    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 11004);
    ubyte sendBytes[] = 
        Encoding.get_ASCII().GetBytes("Is anybody there?");
    try {
        udpClient.Send(sendBytes, sendBytes.length, ipEndPoint);
    }
    catch (System.Exception e) {
        Console.WriteLine(e.ToString());
    }
}
Platforms

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
See Also

Tags :


Page view tracker