XGain #00 – TCP/IP server written in C#

Almost three months ago I’ve started working on Kronos with only one idea – make it as good as I can and have fun. In the past I had the pleasure to work only with high level networking – HTTP(S). Network layer in distributed systems is one of the most sensitive and in case of project with requirement for high performance, HTTP is not the best idea.


.NET framework has a Socket class to deal with low-level networking. Standalone synchronous server based on Socket is not so difficult, here is an example:

Microsoft has provided two ways of creating high performance TCP/IP server. The most cool is SocketAsyncEventArgs with event-based system. In short summary – this model suggest you to create a pool SocketAsyncArgs and use them to processing. Unfortunately I have spotten many problems on Linux and Windows with stability after reusing already created SocketAsyncEventArgs. Probably .NET Core with CoreCLR (RC1) has some issue, let’s wait for RC2 or RTM.

As mentioned earlier, there is also another option – async methods based on IAsyncResult interface (Asynchronous Programming Model). TPL allows us to create Task wrapper around IAsyncResult. TcpListener API is a cool example of that.

TcpListener API is really great – async methods, possibility to choose Socket or TcpClient and more.

My project requires more specific logic of receiving request from client:
1. Wait for new connection
2. Receive request type
3. Send confirmation
4. Receive request
5. Send confirmation
6. Start processing

After few sleepless nights and many conversations about networking programming with Jakub, I’ve decided to abstract standalone TCP layer to another project – XGain. How this server works? How Kronos is interacting with this server? Soon on my blog.

Early version of XGain and Kronos is already available on my github. Feel free to ask, comment and give feedback!