Socket.ReceiveAsync(SocketAsyncEventArgs) Socket.ReceiveAsync(SocketAsyncEventArgs) Socket.ReceiveAsync(SocketAsyncEventArgs) Socket.ReceiveAsync(SocketAsyncEventArgs) Method

定义

开始一个异步请求以便从连接的 Socket 对象中接收数据。Begins an asynchronous request to receive data from a connected Socket object.

public:
 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

参数

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.

异常

参数无效。An argument was invalid. e 参数的 BufferBufferList 属性必须引用有效的缓冲区。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.

已经在使用 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.

注解

ReceiveAsync方法使用在套接字或绑定无连接的套接字连接,用于读取传入的数据。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.

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属性设置为在调用之前所需的任何用户状态对象ReceiveAsync方法,以便可在回调方法中检索信息。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.

对于面向消息的套接字传入的消息会放入缓冲区中直到与关联的缓冲区的总大小e参数。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.

对于面向连接的套接字,ReceiveAsync方法可以指示依赖于套接字是字节流或面向消息的两种方式之一中的虚拟线路正常终止。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. 对于面向消息的套接字,其中零字节的消息通常是允许的SocketExceptionSocketAsyncEventArgs.SocketError设置为本机 Winsock WSAEDISCON 错误代码 (10101) 用于指示正常结束。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. 在任何情况下,SocketExceptionSocketAsyncEventArgs.SocketError错误代码 (10054) 设置为本机 Winsock WSAECONNRESET 指示硬性关闭已发生。In any case, a SocketException with the SocketAsyncEventArgs.SocketError set to the native Winsock WSAECONNRESET error code (10054) indicates an abortive close has occurred.

适用于

另请参阅