다음을 통해 공유


Socket.ReceiveFromAsync Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Begins an asynchronous request to receive data from a specific remote host.

Namespace:  System.Net.Sockets
Assembly:  System.Net (in System.Net.dll)

Syntax

'Declaration
Public Function ReceiveFromAsync ( _
    e As SocketAsyncEventArgs _
) As Boolean
public bool ReceiveFromAsync(
    SocketAsyncEventArgs e
)

Parameters

Return Value

Type: System.Boolean
Returns true if the I/O operation is pending. The 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 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.

Exceptions

Exception Condition
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.

ObjectDisposedException

The Socket has been closed.

SocketException

An error occurred when attempting to access the socket.

Remarks

The ReceiveFromAsync(SocketAsyncEventArgs) method is used primarily to receive data on a connectionless socket of type Dgram. The socket's local address must be known.

The ReceiveFromAsync(SocketAsyncEventArgs) method can also be used to receive data on a connection-oriented socket of type Stream.

The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the IPEndPoint of a 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 of type Stream, the socket must already be connected to the remote address.

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 connectionless sockets of type Dgram, 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 connection-oriented sockets of type Stream, 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.

NoteNote:

If you receive a SocketException when calling this method, use the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets Error Codes documentation in the MSDN library for a detailed description of the error.

Version Notes

Silverlight for the desktop

Do not use for a Silverlight for the desktop application.

Version Information

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.