Socket.ReceiveAsync(SocketAsyncEventArgs) Method


Begins an asynchronous request to receive data from a connected Socket object.

 bool ReceiveAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool ReceiveAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.ReceiveAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function ReceiveAsync (e As SocketAsyncEventArgs) As Boolean



The SocketAsyncEventArgs object to use for this asynchronous socket operation.



true if the I/O operation is pending. The Completed event on the e parameter will be raised upon completion of the operation.

false if the I/O operation completed synchronously. In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.


An argument was invalid. The Buffer or BufferList properties on the e parameter must reference valid buffers. One or the other of these properties may be set, but not both at the same time.

A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

Windows XP or later is required for this method.

The Socket has been closed.

An error occurred when attempting to access the socket.


The ReceiveAsync method is used on connected sockets or bound connectionless sockets and is used to read incoming data. The socket's local address must be known.

For bound connectionless sockets, this function restricts the addresses from which received messages are accepted. The function only returns messages from the remote address specified in the connection. Messages from other addresses are silently discarded.

The SocketAsyncEventArgs.SocketFlags property on the e parameter provides the Window Sockets service provider with additional information about the read request. For more information about how to use this parameter, see System.Net.Sockets.SocketFlags.

The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the ReceiveAsync method, so that the information will be retrievable in the callback method. If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

For byte stream-style sockets, incoming data is placed into the buffer until the buffer is filled, the connection is closed, or the internally buffered data is exhausted.

For message-oriented sockets, an incoming message is placed into the buffer up to the total size of the buffer associated with the e parameter. If the message is larger than the buffer, the buffer is filled with the first part of the message.

For connection-oriented sockets, the ReceiveAsync method can indicate the graceful termination of the virtual circuit in one of two ways that depend on whether the socket is byte stream or message oriented. For byte streams, zero bytes having been read indicates graceful closure and that no more bytes will ever be read. For message-oriented sockets, where a zero byte message is often allowable, a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAEDISCON error code (10101) is used to indicate graceful closure. In any case, a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAECONNRESET error code (10054) indicates an abortive close has occurred.

Applies to

See also