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.

若要在完成时得到通知,必须创建实现事件处理程序的回调方法<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.

如果SocketAsyncEventArgs.AcceptSocket属性为 null,一个新Socket具有相同构造AddressFamilySocketType,并ProtocolType与当前Socket并将设置为SocketAsyncEventArgs.AcceptSocket属性。If 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属性设置为ConnectionResetThe 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.

适用于

另请参阅