Re: Can NetworkStream.Write() block?



Gaz wrote:
myNetworkStream.Write(byte[], int offset, int size);
<snip>
It seems this is better than using the non-blocking version myNetworkStream.BeginWrite(byte[] buffer, int offset, int size) since all that does (if the write buffer is full) is create a backlog of BeginWrite threads all waiting to complete.

This is *not* what happens. The BeginX methods generally do *not* create threads at all to handle the asynchronous requests, because that doesn't scale. Instead, they use overlapped I/O when possible. Do not assume that the asynchronous methods are built on the synchronous methods, because it's generally the other way around.

In general, you can assume asynchronous calls are *more* efficient than their synchronous companions, since those *always* take up a thread (namely the thread waiting for completion) while the asynchronous ones generally only take up a thread when executing the completion callback.

Might as well use the blocking version and increase the buffer size if that is possible.

For overlapped I/O (which .NET uses whenever possible) there's no blocking. The overlapped requests are simply queued until there's an opportunity to process them. In the rare event that the requests (plus their associated buffers) overflow the queue, you'd get a SocketException, but I'm almost certain there's no blocking. (Almost certain -- if a .NET networking guru could pipe up and confirm or deny this, I'd be grateful.)

--
J.
.



Relevant Pages

  • Re: AWR Sample Report
    ... Shared Pool Size: 1,264M 1,264M Log Buffer: 14,344K ... Cached Commit SCN referenced 35,858,913 1,662.63 1,476.95 ... IMU Redo allocation size 11,813,948 547.76 486.59 ... physical read IO requests 5,242,302 243.06 215.92 ...
    (comp.databases.oracle.server)
  • Re: serving a file to a client --- background fcopy read fileevent event puts socket cha
    ... invokes a callback when there's both data waiting and somewhere to ... probably not -size (although having -size pre-load the input buffer ... you can't even observe the progress of the [fcopy] from outside (ie. ... The division of functionality I mentioned should have negligible impact ...
    (comp.lang.tcl)
  • Re: InputStream
    ... Especially if reading from a network connection or ... > The displayed message always says that the read amount is lower than the ... read(bytebuffer, int offset, int maxWanted) is for doing this. ...
    (comp.lang.java.programmer)
  • Completion ports and sockets - scaling my app
    ... First chunk 1 is put on his way by WSASend. ... Such an application must, however, take great care to ensure that it posts multiple overlapped sends, instead of waiting for one overlapped send to complete before posting another. ... If it had another buffer already posted, the transport would be able to use that buffer immediately and ... By implementing some kind of sequence numbering scheme? ...
    (microsoft.public.win32.programmer.networks)
  • XFS related hang (was Re: How to send a break? - dump from frozen 64bit linux)
    ... we're waiting here on a synchronisation variable that'll ... that'd be called by the I/O completion handler on the buffer ... waiting for the driver to wake us up when its done. ... we aren't seeing the I/O completion here for some reason... ...
    (Linux-Kernel)