Socket.EndReceive Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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);
public int EndReceive (IAsyncResult asyncResult);
member this.EndReceive : IAsyncResult -> int
Public Function EndReceive (asyncResult As IAsyncResult) As Integer
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
.
asyncResult
was not returned by a call to the BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) method.
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
Important
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.
Note
If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.
Note
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.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.
See also
- BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- IAsyncResult
- AsyncState
- Shutdown(SocketShutdown)
Applies to
EndReceive(IAsyncResult, SocketError)
- Source:
- Socket.cs
- Source:
- Socket.cs
- Source:
- Socket.cs
Ends a pending asynchronous read.
public:
int EndReceive(IAsyncResult ^ asyncResult, [Runtime::InteropServices::Out] System::Net::Sockets::SocketError % errorCode);
public int EndReceive (IAsyncResult asyncResult, out System.Net.Sockets.SocketError errorCode);
member this.EndReceive : IAsyncResult * SocketError -> int
Public Function EndReceive (asyncResult As IAsyncResult, ByRef errorCode As SocketError) As Integer
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
.
asyncResult
was not returned by a call to the BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) method.
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
Important
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.
Note
If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code.
Note
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.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.
See also
- BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- IAsyncResult
- AsyncState
- Shutdown(SocketShutdown)