UdpClient.BeginSend Method

Definition

Sends a datagram to a remote host asynchronously.

Overloads

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

Sends a datagram to a destination asynchronously. The destination is specified by the host name and port number.

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

Sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.

BeginSend(Byte[], Int32, AsyncCallback, Object)

Sends a datagram to a remote host asynchronously. The destination was specified previously by a call to Connect.

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a datagram to a destination asynchronously. The destination is specified by the host name and port number.

C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, string? hostname, int port, AsyncCallback? requestCallback, object? state);
C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, string hostname, int port, AsyncCallback requestCallback, object state);

Parameters

datagram
Byte[]

A Byte array that contains the data to be sent.

bytes
Int32

The number of bytes to send.

hostname
String

The destination host.

port
Int32

The destination port number.

requestCallback
AsyncCallback

An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

A user-defined object that contains information about the send operation. This object is passed to the requestCallback delegate when the operation is complete.

Returns

An IAsyncResult object that references the asynchronous send.

Examples

The following code example uses BeginSend to asynchronously send a server request.

C#
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
C#
static void SendMessage3(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the server name and port
    u.BeginSend(sendBytes, sendBytes.Length, server, s_listenPort, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

Remarks

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the Send method overloads.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a datagram to a destination asynchronously. The destination is specified by a EndPoint.

C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, System.Net.IPEndPoint? endPoint, AsyncCallback? requestCallback, object? state);
C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, System.Net.IPEndPoint endPoint, AsyncCallback requestCallback, object state);

Parameters

datagram
Byte[]

A Byte array that contains the data to be sent.

bytes
Int32

The number of bytes to send.

endPoint
IPEndPoint

The EndPoint that represents the destination for the data.

requestCallback
AsyncCallback

An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

A user-defined object that contains information about the send operation. This object is passed to the requestCallback delegate when the operation is complete.

Returns

An IAsyncResult object that references the asynchronous send.

Examples

The following code example uses BeginSend to asynchronously send a server request.

C#
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
C#
static void SendMessage2(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // resolve the server name
    IPHostEntry heserver = Dns.GetHostEntry(server);

    IPEndPoint e = new IPEndPoint(heserver.AddressList[0], s_listenPort);

    // send the message
    // the destination is defined by the IPEndPoint
    u.BeginSend(sendBytes, sendBytes.Length, e, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

Remarks

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation is complete. To block until the operation is complete, use one of the Send method overloads.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

BeginSend(Byte[], Int32, AsyncCallback, Object)

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

Sends a datagram to a remote host asynchronously. The destination was specified previously by a call to Connect.

C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, AsyncCallback? requestCallback, object? state);
C#
public IAsyncResult BeginSend(byte[] datagram, int bytes, AsyncCallback requestCallback, object state);

Parameters

datagram
Byte[]

A Byte array that contains the data to be sent.

bytes
Int32

The number of bytes to send.

requestCallback
AsyncCallback

An AsyncCallback delegate that references the method to invoke when the operation is complete.

state
Object

A user-defined object that contains information about the send operation. This object is passed to the requestCallback delegate when the operation is complete.

Returns

An IAsyncResult object that references the asynchronous send.

Examples

The following code example uses BeginSend to asynchronously send a server request.

C#
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
C#
static void SendMessage1(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    u.Connect(server, s_listenPort);
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the call to .Connect()
    u.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

Remarks

The asynchronous BeginSend operation must be completed by calling the EndSend method. Typically, the method is invoked by the requestCallback delegate.

This method does not block until the operation completes. To block until the operation is complete, use one of the Send method overloads.

For detailed information about using the asynchronous programming model, see Calling Synchronous Methods Asynchronously.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1