Socket.EndReceive Method

Definition

Ends a pending asynchronous read.

Overloads

EndReceive(IAsyncResult)

Ends a pending asynchronous read.

EndReceive(IAsyncResult, SocketError)

Ends a pending asynchronous read.

EndReceive(IAsyncResult)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Ends a pending asynchronous read.

public int EndReceive (IAsyncResult asyncResult);

Parameters

asyncResult
IAsyncResult

An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

Returns

The number of bytes received.

Exceptions

asyncResult is null.

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

An error occurred when attempting to access the socket.

-or-

.NET 7+ only: The Socket has been closed.

.NET Framework, .NET Core, and .NET 5-6 only: The Socket has been closed.

Remarks

Viktigt

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

EndReceive completes the operation started by BeginReceive. You need to pass the IAsyncResult created by the matching BeginReceive call.

The EndReceive method will block until data is available. If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. If you are using a connection-oriented protocol, the EndReceive method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceive method. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes.

Anteckning

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Anteckning

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Anteckning

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 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

EndReceive(IAsyncResult, SocketError)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Ends a pending asynchronous read.

public int EndReceive (IAsyncResult asyncResult, out System.Net.Sockets.SocketError errorCode);

Parameters

asyncResult
IAsyncResult

An IAsyncResult that stores state information and any user defined data for this asynchronous operation.

errorCode
SocketError

A SocketError object that stores the socket error.

Returns

The number of bytes received.

Exceptions

asyncResult is null.

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

An error occurred when attempting to access the socket.

-or-

.NET 7+ only: The Socket has been closed.

.NET Framework, .NET Core, and .NET 5-6 only: The Socket has been closed.

Remarks

Viktigt

This is a compatibility API. We don't recommend using the APM (Begin* and End*) methods for new development. Instead, use the Task-based equivalents.

EndReceive completes the operation started by BeginReceive. You need to pass the IAsyncResult created by the matching BeginReceive call.

The EndReceive method will block until data is available. If you are using a connectionless protocol, EndReceive will read the first enqueued datagram available in the incoming network buffer. If you are using a connection-oriented protocol, the EndReceive method will read as much data as is available up to the number of bytes you specified in the size parameter of the BeginReceive method. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the EndReceive method will complete immediately and return zero bytes.

Anteckning

If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.

Anteckning

All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes.

Anteckning

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

.NET 9 och andra versioner
Produkt Versioner
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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