Socket類有一個方法.AcceptAsync,它返回true或false。異步套接字 - 處理false socket.AcceptAsync值
我以爲假返回值是一個錯誤情況,但在樣品中微軟提供了他們調用回調函數檢查失敗後同步異步插座,如下所示:
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);
}
爲什麼他們這樣做?它破壞了異步套接字的用途並停止了方法返回。
關鍵是要將兩個完全不同的行爲綁定到一個似乎只明確指出一個的函數。它被徹底記錄的好事情......等等。我分享你的挫折感。 – QueueHammer 2010-03-16 19:49:10