Socket.AcceptAsync(SocketAsyncEventArgs) Socket.AcceptAsync(SocketAsyncEventArgs) Socket.AcceptAsync(SocketAsyncEventArgs) Socket.AcceptAsync(SocketAsyncEventArgs) Method

定义

开始一个异步操作来接受一个传入的连接尝试。Begins an asynchronous operation to accept an incoming connection attempt.

public:
 bool AcceptAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool AcceptAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.AcceptAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function AcceptAsync (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 对象以检索操作的结果。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 is not valid. 如果所提供的缓冲区不够大,将会发生此异常。This exception occurs if the buffer provided is not large enough. 缓冲区必须至少为 2 * (sizeof(SOCKADDR_STORAGE + 16) 字节。The buffer must be at least 2 * (sizeof(SOCKADDR_STORAGE + 16) bytes.

如果指定了多个缓冲区,即 BufferList 属性不为 null,也会发生此异常。This exception also occurs if multiple buffers are specified, the BufferList property is not null.

参数超出范围。An argument is out of range. 如果 Count 小于 0,将会发生此异常。The exception occurs if the Count is less than 0.

请求了无效操作。An invalid operation was requested. 如果接收方 Socket 未侦听连接或者绑定了接受的套接字,将发生此异常。This exception occurs if the accepting Socket is not listening for connections or the accepted socket is bound.

Bind(EndPoint)Listen(Int32) 方法必须先于 AcceptAsync(SocketAsyncEventArgs) 方法调用。You must call the Bind(EndPoint) and Listen(Int32) method before calling the AcceptAsync(SocketAsyncEventArgs) method.

如果套接字已连接或使用指定的 e 参数的套接字操作已经在进行中,也会发生此异常。This exception also occurs if the socket is already connected or a socket operation was already in progress using the specified e parameter.

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

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

注解

面向连接的协议可以使用AcceptAsync方法异步处理传入的连接尝试。Connection-oriented protocols can use the AcceptAsync method to asynchronously process incoming connection attempts. 异步接受连接使你能够在单独的执行线程中发送和接收数据。Accepting connections asynchronously gives you the ability to send and receive data within a separate execution thread. 在调用AcceptAsync方法之前, 必须Listen调用方法以侦听传入连接请求并对其进行排队。Before calling the AcceptAsync method, you must call the Listen method to listen for and queue incoming connection requests.

若要在完成时收到通知, 您必须创建一个回调方法, 用于<实现 EventHandler SocketAsyncEventArgs > 委托, 并将SocketAsyncEventArgs.Completed其挂钩到事件。To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and hook it to the SocketAsyncEventArgs.Completed event.

System.Net.Sockets.SocketAsyncEventArgs对象上的下列属性和事件是必需的:The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required:

调用方可以选择指定Socket Socket要用于传入连接的现有, 方法是指定要与SocketAsyncEventArgs.AcceptSocket属性一起使用的。The caller can optionally specify an existing Socket to use for the incoming connection by specifying the Socket to use with the SocketAsyncEventArgs.AcceptSocket property.

SocketType Socket SocketAsyncEventArgs.AcceptSocket ProtocolType如果属性为 null, 则使用与当前AddressFamily相同的、和来构造新的,并将设置为属性。Socket SocketAsyncEventArgs.AcceptSocketIf the SocketAsyncEventArgs.AcceptSocket property is null, a new Socket is constructed with the same AddressFamily, SocketType, and ProtocolType as the current Socket and set as the SocketAsyncEventArgs.AcceptSocket property.

调用方可以在SocketAsyncEventArgs.UserToken AcceptAsync调用方法之前将属性设置为所需的任何用户状态对象, 以便可以在回调方法中检索信息。The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the AcceptAsync 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.

(可选) 可以在ConnectAsync方法成功后, 提供缓冲区来接收套接字上的初始数据块。Optionally, a buffer may be provided in which to receive the initial block of data on the socket after the ConnectAsync method succeeds. 在这种情况下SocketAsyncEventArgs.Buffer , 需要将属性设置为包含要接收数据的缓冲区SocketAsyncEventArgs.Count , 并且需要将属性设置为要在缓冲区中接收的数据的最大字节数。In this case, the SocketAsyncEventArgs.Buffer property needs to be set to the buffer containing the data to receive and the SocketAsyncEventArgs.Count property needs to be set to the maximum number of bytes of data to receive in the buffer. 可以使用SocketAsyncEventArgs.SetBuffer方法设置这些属性。These properties can be set using the SocketAsyncEventArgs.SetBuffer method. 传入的缓冲区部分将在内部使用, 供基础 Winsock AcceptEx 调用使用。Part of the buffer passed in will be consumed internally for use by the underlying Winsock AcceptEx call. 这意味着返回的数据量将始终小于提供的SocketAsyncEventArgs.Count System.Net.Sockets.SocketAsyncEventArgs实例的属性值。This means that the amount of data returned will always be less than the value of the SocketAsyncEventArgs.Count property on the System.Net.Sockets.SocketAsyncEventArgs instance provided. 基于套接字的地址族, 在内部使用的缓冲区量会有所不同。The amount of the buffer used internally varies based on the address family of the socket. 所需的最小缓冲区大小为288个字节。The minimum buffer size required is 288 bytes. 如果指定较大的缓冲区大小, 则Socket将期望一些额外的数据 (而不是 Winsock AcceptEx 调用接收到的地址数据), 并将等待, 直到收到此额外的数据。If a larger buffer size is specified, then the Socket will expect some extra data other than the address data received by the Winsock AcceptEx call and will wait until this extra data is received. 如果发生超时, 则会重置连接。If a timeout occurs, the connection is reset. 因此, 如果特定金额需要额外数据, 则应将缓冲区大小设置为最小缓冲区大小加上此量。So if extra data is expected of a specific amount, then the buffer size should be set to the minimum buffer size plus this amount.

完成回调方法应检查SocketAsyncEventArgs.SocketError属性, 以确定AcceptAsync操作是否成功。The completion callback method should examine the SocketAsyncEventArgs.SocketError property to determine if the AcceptAsync operation was successful.

SocketAsyncEventArgs.Completed某些情况下, 如果未接受连接并且SocketAsyncEventArgs.SocketError使属性设置为ConnectionReset, 则可能发生此事件。The SocketAsyncEventArgs.Completed event can occur in some cases when no connection has been accepted and cause the SocketAsyncEventArgs.SocketError property to be set to ConnectionReset. 这种情况可能是通过使用半开放式 SYN 类型扫描 (SYN > SYN-ACK-> RST 序列) 进行端口扫描引起的。This can occur as a result of port scanning using a half-open SYN type scan (a SYN -> SYN-ACK -> RST sequence). 应准备使用AcceptAsync方法的应用程序来处理这种情况。Applications using the AcceptAsync method should be prepared to handle this condition.

适用于

另请参阅