UdpClient.EndSend(IAsyncResult) Method

Definition

Ends a pending asynchronous send.

C#
public int EndSend(IAsyncResult asyncResult);

Parameters

asyncResult
IAsyncResult

An IAsyncResult object returned by a call to BeginSend.

Returns

If successful, the number of bytes sent to the UdpClient.

Exceptions

asyncResult is null.

asyncResult was not returned by a call to the BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) method.

EndSend(IAsyncResult) was previously called for the asynchronous read.

An error occurred when attempting to access the underlying socket.

The underlying Socket has been closed.

Examples

The following code example uses BeginSend to complete an asynchronous send of 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;
}

Remarks

This method blocks until the operation is complete.

To perform this operation synchronously, use the Send method.

Applies to

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