Socket.EndReceive Method
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.
EndReceive(IAsyncResult) |
Ends a pending asynchronous read. |
EndReceive(IAsyncResult, SocketError) |
Ends a pending asynchronous read. |
- 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
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
- BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- IAsyncResult
- AsyncState
- Shutdown(SocketShutdown)
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 |
- 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
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
- BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object)
- AsyncCallback
- IAsyncResult
- AsyncState
- Shutdown(SocketShutdown)
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 |
Feedback om .NET
.NET är ett öppen källkod projekt. Välj en länk för att ge feedback: