Asynchronous TCP Sockets as an Alternative to WCF

I wrote an article “Asynchronous TCP Sockets as an Alternative to WCF” which appears in the March 2014 issue of MSDN Magazine. See Most developers are comfortable with the concept of client-server programming. But the details of actually coding a client-server system are very, very tricky. In the old C++ days (not sure exactly when), if a developer wanted to implement a client-server system, he’d use Sockets. The term Socket can have several different meanings but in most cases a Socket is a code combination of a TCP address, a port number, and a set of protocol guidelines that describe how the client and server will communicate. There are several socket libraries.

Socket programming is extremely difficult. And if a developer wants to introduce asynchronous operations on the client or the server, he’s in for a world of pain.

Over the past ten years or so, there have been many new libraries that are essentially wrappers around raw socket programming, designed to make client-server programming somewhat easier. These include the .NET TcpClient class, HttpClient class, ASP.NET Web Services, its successor WCF, the new Web API (what a horrible name), AJAX programming, and named pipe programming (an old technology).

These technologies made client-server programming much easier. But introducing asynchronous behavior is still nutso difficult. In my article, I show an example of how to write a simple client-server system using TcpClient (to hide socket complexity) and the new C# await and async keywords (to hide async complexity). The client is almost completely asynchronous, meaning users can submit requests and then continue until the server responds. The server is mostly asynchronous, meaning the server can accept multiple requests and queue them up internally, but the requests are processed one by one in the order in which they arrive.

My conclusion is that WCF, even though I hate it with a passion, is the best choice for complex client-server scenarios, but for simple scenarios where security is not a major problem, using asynchronous sockets with the new async and await keywords is a reasonable alternative.


This entry was posted in Miscellaneous. Bookmark the permalink.