I am trying to create an asynchronous tcp server which can handle multiple concurrent connections. Then, I have read through the example shown in MSDN:
https://msdn.microsoft.com/en-us/library/fx6588te.aspx
But it happpened that I change the asynchronous receive in AcceptCallback function to synchronous receive, so now my AcceptCallback function become:
private void AcceptCallback(IAsyncResult ar)
{
allDone.Set();
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Sync receive
byte[] buff = new byte[1024];
handler.Receive(buff);
ConsumeMessage(handler, buff);
}
Then, my tcp server is not able to handle multiple concurrent connections anymore. If there are two concurrent connections, it can only connect one client, and the other one still in connection queue. After looking around, I found this article:
Asynchronous server socket multiple clients
Groo mentioned that: "To allow mulitple concurrent connections, you need to create a receive socket using handler.BeginReceive, and then call listener.BeginAccept to start listening to new clients."
Can anyone explain why we need to use AsyncReceive here to be able to handle multiple connections? If I want to use SyncReceive, is there any way to make it still able to handle multiple connections?