(0) exportieren Drucken
Alle erweitern

Socket.ReceiveFromAsync Method

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

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

public bool ReceiveFromAsync(
	SocketAsyncEventArgs e
)

Parameters

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

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.

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.

ObjectDisposedException

The Socket has been closed.

SocketException

An error occurred when attempting to access the socket.

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.

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Community-Beiträge

Microsoft führt eine Onlineumfrage durch, um Ihre Meinung zur MSDN-Website zu erfahren. Wenn Sie sich zur Teilnahme entscheiden, wird Ihnen die Onlineumfrage angezeigt, sobald Sie die MSDN-Website verlassen.

Möchten Sie an der Umfrage teilnehmen?
Anzeigen:
© 2014 Microsoft