Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen does Socket.SendAsync complete synchronously?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/14344947/socket-beginsend-does-not-complete-synchronously-despite-plenty-of-buffer-space">Socket.BeginSend does not complete synchronously despite plenty of buffer space</a> </p> </blockquote> <p>Documentation for <a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.sendasync.aspx" rel="nofollow noreferrer"><code>Socket.SendAsync</code></a> says that its return value indicates either pending asynchronous operation (returns true) or an immediately completed operation (returns false). </p> <p>In my case the return value is always true: all send requests result in an asynchronous callback when complete and <code>SendAsync</code> never completes synchronously.</p> <p>Why doesn't it just put the data in the 8KB system send buffer and return? What is it waiting for?</p> <p>You can see it with simple test case (cleanup code stripped):</p> <pre><code>TcpListener listener = new TcpListener(IPAddress.Any, 35001); listener.Start(); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.Connect(new IPEndPoint(IPAddress.Loopback, 35001)); Socket server = listener.AcceptSocket(); SocketAsyncEventArgs asyncArgs = new SocketAsyncEventArgs(); asyncArgs.SetBuffer(new byte[] { 1, 2, 3 }, 0, 3); bool pending = client.SendAsync(asyncArgs); Assert.IsFalse(pending); // fails - asynchronous operation is pending </code></pre> <p>My motivation is performance. I would like to have most sends completed synchronously since they are a little faster. Without synchronous completion, I would have to handle 2 million asynchronous callbacks per second in addition to already CPU-intensive application logic.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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