Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous TCP Communication in .NET

Quick question here: is there any obvious benefit to use asynchronous communication with the NetworkStream class (generated by TcpClient), i.e. the BeginRead/BeginWrite methods rather than running a separate thread and using synchronous operations on that, i.e. Read/Write? My impression has been (it could easily be quite wrong) that the asynchronous operations are non-blocking and performed at OS-level (in the TCP stack perhaps?), with a thread pool item for the callback. I am thinking that it surely must be different from calling ThreadPool.QueueUserWorkItem on the synchronous method, or it there would be rather little point in providing it. Now, I'm fairly confident this is the sort of thing (OS-level calls) that happens for file I/O at least, but if someone could please clarify the matter regarding network (TCP) communication, it would be most helpful. Basically, I would like to know whether there's any specific benefit to either method (besides the obvious one of being able to use the BinaryReader/StreamReader classes with the synchronous calls).

like image 249
Noldorin Avatar asked Feb 20 '09 22:02

Noldorin


3 Answers

There is a difference, if you use a Worker thread to call the synchronous version you will tie up one of the threads on a blocking call.

Whereas the Begin methods will not tie up a thread but will instead use a callback on an appropriate I/O signal, the call back will then run on a thread from the pool.

like image 77
AnthonyWJones Avatar answered Oct 13 '22 21:10

AnthonyWJones


I agree with AnthonyWJones, imagine your thread pool has 10 threads, but you have 100 passive enought clients. With async calls you can BeginRead for every one of them, and when data from some one is ready, it will be processed by one of the pool threads. But if you will try to use QueueUserWorkItem, you'll schedule data receiving from only 10 clients. And if they send nothing in 1 hour, other 90 clients will never get a chance to get data.

like image 1
alex2k8 Avatar answered Oct 13 '22 23:10

alex2k8


I'm not really sure why NetworkStream even has a BeginRead/Write, since this basically violates the purpose of the NetworkStream in the first place. By using the Async methods, you gain faster response, greater scalability, and reduced resource consumption.

If you are only ever going to have one connection at a time, then it doesn't matter much if you use a thread pool thread or not, but if you accept many connections then you definitely want to use async.

like image 1
Erik Funkenbusch Avatar answered Oct 13 '22 22:10

Erik Funkenbusch