Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Socket.ReceiveFromAsync Method

Begins to asynchronously receive data from a specified network device.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public bool ReceiveFromAsync(
	SocketAsyncEventArgs e
)

Parameters

e
Type: System.Net.Sockets.SocketAsyncEventArgs
The System.Net.Sockets.SocketAsyncEventArgs object to use for this asynchronous socket operation.

Return Value

Type: System.Boolean
Returns true if the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the e parameter will be raised upon completion of the operation.
Returns false if the I/O operation completed synchronously. In this case, The SocketAsyncEventArgs.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.
ExceptionCondition
ArgumentNullException

The SocketAsyncEventArgs.RemoteEndPoint cannot be null.

InvalidOperationException

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

NotSupportedException

Windows XP or later is required for this method.

ObjectDisposedException

The Socket has been closed.

SocketException

An error occurred when attempting to access the socket.

The ReceiveFromAsync method is used primarily to receive data on a connectionless socket. The socket's local address must be known.

The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the IPEndPoint of a remote host. However, for a connectionless socket, data received from any remote host is accepted. After a successful call to the ReceiveFromAsync(SocketAsyncEventArgs) method on a connectionless socket, the SocketAsyncEventArgs.RemoteEndPoint property will contain the remote host that sent the data.

For a connection-oriented socket for a client application, the socket must already be connected to the remote address before calling the ReceiveFromAsync(SocketAsyncEventArgs) method. For a connection-oriented socket for a server application, the socket must already be bound and listening on a local address before calling the ReceiveFromAsync(SocketAsyncEventArgs) method. For a connection-oriented socket, the value set for the SocketAsyncEventArgs.RemoteEndPoint property is ignored. For a connection-oriented socket for a client application, the remote host is overridden by the value set when the connection was established. For a connection-oriented socket for a server application listening for connections, data received any remote host is accepted.

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 ReceiveFromAsync 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 message-oriented sockets, an incoming message is placed into the buffer up to the total size of the buffer. The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

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. The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP1, 2.0 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.