gpt4 book ai didi

.net - 异步套接字-处理错误的socket.AcceptAsync值

转载 作者:行者123 更新时间:2023-12-03 11:50:55 27 4
gpt4 key购买 nike

Socket类具有方法.AcceptAsync,该方法返回true或false。

我本以为错误的返回值是一个错误情况,但是在Microsoft提供的Async套接字示例中,它们在检查失败后同步调用了回调函数,如下所示:

public void StartAccept(SocketAsyncEventArgs acceptEventArg)
{
if (acceptEventArg == null)
{
acceptEventArg = new SocketAsyncEventArgs();
acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);
}
else
{
// socket must be cleared since the context object is being reused
acceptEventArg.AcceptSocket = null;
}

m_maxNumberAcceptedClients.WaitOne();
bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);
if (!willRaiseEvent)
{
ProcessAccept(acceptEventArg);
}
}

/// <summary>
/// This method is the callback method associated with Socket.AcceptAsync operations and is invoked
/// when an accept operation is complete
/// </summary>
void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)
{
ProcessAccept(e);
}

他们为什么这样做呢?它违反了异步套接字的目的,并阻止了该方法的返回。

最佳答案

根据AcceptAsync规范:

Returns false if the I/O operation completed synchronously. The SocketAsyncEventArgs.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.



当它返回false时,您应该立即处理新接受的套接字。

关于.net - 异步套接字-处理错误的socket.AcceptAsync值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2002143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com