Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><s>Well, you have a period of time between when you accept a connection and when the connection is "made" before you can accept another connection. Using things like wait handles is probably fairly slow.</s></p> <p>After re-reading your code, conceptually the point at which you can accept another connection is when you've called <code>EndAccept</code>. It appears that you're setting the event before you call <code>EndAccept</code> which means <code>BeginAccept</code> can be called before the <code>EndAccept</code> and that another connection can be accepted before the previous had <code>EndAccept</code> called. I don't know if that's an issue--as far as I can tell, that's legal. But, you can simplify your code to avoid the events and simply chain the next BeginAccept during the current accept and ensure <code>EndAccept</code> is called before the next <code>BeginAccept</code><s></p> <p>What I do is chain the next accept from within the current accept. </s>For example:</p> <pre><code>listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listener.Bind(new IPEndPoint(IPAddress.Any, 62000)); listener.Listen(1000); listener.BeginAccept(OnAccept, listener); private void OnAccept(IAsyncResult ar) { Socket listener = (Socket)ar.AsyncState; Socket socket = listener.EndAccept(ar); listener.BeginAccept(OnAccept, listener); socket.BeginReceive(new byte[10], 0, 10, 0, (arReceive) =&gt; socket.EndReceive(arReceive), null); } </code></pre> <p>of course, i'm using <code>BeginAcceptSocket</code> here; but the concept would be the same with <code>BeginAccept</code>...</p> <p>This frees you from having to start a new <code>Task</code>, frees from creating wait handles which are about 50-times slower that <code>lock</code> because they are cross-process<s>--which frees you from having a "huge" pause between accepts</s>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload