Socket.ReceiveFromAsync(SocketAsyncEventArgs) Socket.ReceiveFromAsync(SocketAsyncEventArgs) Socket.ReceiveFromAsync(SocketAsyncEventArgs) Socket.ReceiveFromAsync(SocketAsyncEventArgs) Method

定义

开始从指定网络设备中异步接收数据。Begins to asynchronously receive data from a specified network device.

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

参数

e
SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs

要用于此异步套接字操作的 SocketAsyncEventArgs 对象。The SocketAsyncEventArgs object to use for this asynchronous socket operation.

返回

如果 I/O 操作挂起,则为 truetrue if the I/O operation is pending. 操作完成时,将引发 e 参数的 Completed 事件。The Completed event on the e parameter will be raised upon completion of the operation.

如果 I/O 操作同步完成,则为 falsefalse if the I/O operation completed synchronously. 在这种情况下,将不会引发 e 参数的 Completed 事件,并且可能在方法调用返回后立即检查作为参数传递的 e 对象以检索操作的结果。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.

异常

已经在使用 e 参数中指定的 SocketAsyncEventArgs 对象执行套接字操作。A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

此方法需要 Windows XP 或更高版本。Windows XP or later is required for this method.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

注解

ReceiveFromAsync方法主要用于在无连接的套接字上接收数据。The ReceiveFromAsync method is used primarily to receive data on a connectionless socket. 必须知道套接字的本地地址。The socket's local address must be known.

调用方必须设置SocketAsyncEventArgs.RemoteEndPoint属性设置为IPEndPoint,数据将从其接收的远程主机。The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the IPEndPoint of the remote host from which the data is to be received.

SocketAsyncEventArgs.SocketFlags属性上的e参数提供了关于读取请求的其他信息的窗口套接字服务提供程序。The SocketAsyncEventArgs.SocketFlags property on the e parameter provides the Window Sockets service provider with additional information about the read request. 有关如何使用此参数的详细信息,请参阅System.Net.Sockets.SocketFlagsFor more information about how to use this parameter, see System.Net.Sockets.SocketFlags.

下列属性和事件上的System.Net.Sockets.SocketAsyncEventArgs成功调用此方法所需的对象:The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

调用方可能会设置SocketAsyncEventArgs.UserToken属性设置为在调用之前所需的任何用户状态对象ReceiveFromAsync方法,以便可在回调方法中检索信息。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. SocketAsyncEventArgs.CountSocketAsyncEventArgs.Offset属性确定在缓冲区中数据的放置位置和数据量。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. SocketAsyncEventArgs.CountSocketAsyncEventArgs.Offset属性确定在缓冲区中数据的放置位置和数据量。The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

适用于

另请参阅