Sync Vs. Async Sockets Performance in .NET

Posted by Michael Covelli on Stack Overflow See other posts from Stack Overflow or by Michael Covelli
Published on 2010-03-25T00:10:20Z Indexed on 2010/03/25 0:43 UTC
Read the original article Hit count: 447

Filed under:
|
|
|
|

Everything that I read about sockets in .NET says that the asynchronous pattern gives better performance (especially with the new SocketAsyncEventArgs which saves on the allocation).

I think this makes sense if we're talking about a server with many client connections where its not possible to allocate one thread per connection. Then I can see the advantage of using the ThreadPool threads and getting async callbacks on them.

But in my app, I'm the client and I just need to listen to one server sending market tick data over one tcp connection. Right now, I create a single thread, set the priority to Highest, and call Socket.Receive() with it. My thread blocks on this call and wakes up once new data arrives.

If I were to switch this to an async pattern so that I get a callback when there's new data, I see two issues

  1. The threadpool threads will have default priority so it seems they will be strictly worse than my own thread which has Highest priority.

  2. I'll still have to send everything through a single thread at some point. Say that I get N callbacks at almost the same time on N different threadpool threads notifying me that there's new data. The N byte arrays that they deliver can't be processed on the threadpool threads because there's no guarantee that they represent N unique market data messages because TCP is stream based. I'll have to lock and put the bytes into an array anyway and signal some other thread that can process what's in the array. So I'm not sure what having N threadpool threads is buying me.

Am I thinking about this wrong? Is there a reason to use the Async patter in my specific case of one client connected to one server?

© Stack Overflow or respective owner

Related posts about sockets

Related posts about .NET