High performance TCP server in C#

Tom picture Tom · May 16, 2011 · Viewed 57.2k times · Source

I am an experienced C# developer, but I have not developed a TCP server application so far. Now I have to develop a highly scalable and high performance server that can handle at least 5-10 thousand concurrent connections: getting -raw byte- data via GPRS from GPS devices.

A common communication process should look like this:

  • GPS device initiates a connection to my server
  • my server answers if I want to get data
  • device send GPS data
  • my server sends report to the device about getting it (sg like checksum)
  • getting new data from GPS, reportm and this happens again and again
  • later GPS DEVICE closes the connection

So, in my server I need

  • trace connected/active clients
  • to close any client from server side
  • catch the event, when a device closes the connection
  • get byte data
  • send data to clients

I started to read about this topic over the internet, but it seems to be a nightmare for me. There are a lot of ways, but I could not find out which is the best.

Async socket methods seems the best for me, but writing code in this async style is terrible and not easy to debug.

So my question is: which do you think the best way to implement a high performance TCP server in C#? Do you know any good open source component to do this? (I tried several ones, but I could not find a good one.)

Answer

Remus Rusanu picture Remus Rusanu · May 16, 2011

It must be async, there is no way around this. High performance and scalability don't mix with one-thread-per-socket. You can have a look at what StackExchange themselves are doing, see async Redis await BookSleeve which leverages the CTP features from the next C# release (so is on the edge and subject to changes, but it is cool). For even more bleeding edge the solutions evolves around leveraging SocketAsyncEventArgs Class which takes things one step further by eliminating the frequent allocations of async handlers associated with 'classic' C# async processing:

The SocketAsyncEventArgs class is part of a set of enhancements to the System.Net.Sockets.Socket class that provide an alternative asynchronous pattern that can be used by specialized high-performance socket applications. This class was specifically designed for network server applications that require high performance. An application can use the enhanced asynchronous pattern exclusively or only in targeted hot areas (for example, when receiving large amounts of data).

Long story short: learn async or die trying...

BTW, if you're asking why async, then read the three articles linked from this post: High Performance Windows programs. The ultimate answer is: the underlying OS design requires it.