Home > Uncategorized > async and await in Socket

async and await in Socket

async/await improvement in .NET 4.5 / C# 5 is very major changes. I using socket extensively lately to build real-time data transport. The performance is absolutely critical in this area. I try to find the improvement in raw Socket in System.Net.Sockets. But there’s no awaitable method for Accept, Receive and Send. Unfortunately the async method is not what you think it is. It’s EAP not awaitable method.

image

 

image

 

image

 

I and my friend discussing about this yesterday and use the very simple solution to solve this problem. We combine the TPL and APM from .NET. The idea in this stackoverflow post will show you the implementation.

Replacing Socket.ReceiveAsync with NetworkStream.ReadAsync (awaitable)

 

But wait, don’t just jump to implement that just yet. Please read this valuable blog post from the Parallel Team in .NET

Awaiting Socket Operations

There are a lot  of things need to be considered right ? Also another article about this need to be read also and that’s the basic of the rationale for the previous blog post.

Socket Performance Enhancements in Version 3.5

So are you ready to make your socket awaitable ?

Less blocking thread, more performance Smile

Cheers

Categories: Uncategorized
  1. mmajcica
    February 26, 2014 at 4:34 pm

    Hi there, I just bumped into this post, searching if I can improve perf of my library. I had the same idea as you guys had. Is there anything you came up with?

    Thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: