Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a highly scalable TCP/IP server in C# 5 with the async/await pattern?

I'm tasked with designing a fairly simple TCP/IP server that must accept connections from multiple clients. It needs to be written in C#, and I'm using .NET 4.5. That said, I'm not sure what is the current "state of the art" for TCP/IP server/client scalability in .NET 4.5.

I did see this post: How to write a scalable Tcp/Ip based server. But that relates to .NET 2.0 and 3.5 and makes no mention of the async/await pattern.

I am capable of writing a server the "old way"... but I want to know what the "new way" is.

  • What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?
  • Do the new Async methods leverage I/O Completion Ports?
  • Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?

EDIT: Additional questions.

like image 784
Ben Lesh Avatar asked Jan 31 '13 15:01

Ben Lesh


People also ask

How do I run a client server program in C on the same machine?

First, a socket will be created (similar to the client program). Next, the IP and port of the socket will be bound using the bind() function (client used to connect() function to connect to the server; the server will use the bind() function to listen for the connections).

What is TCP IP socket programming?

A socket programming interface provides the routines required for interprocess communication between applications, either on the local system or spread in a distributed, TCP/IP based network environment. Once a peer-to-peer connection is established, a socket descriptor is used to uniquely identify the connection.

What is TCP Echo server?

TCP Echo Server In the TCP Echo server , we create a socket and bind to a advertized port number. After binding , the process listens for incoming connections. Then an infinite loop is started to process the client requests for connections.


1 Answers

What is the best way to use the new Async methods on Socket, TcpClient or TcpListener to create a scalable server in C#?

There aren't any new async methods on Socket; the methods named *Async on Socket are a special set of APIs to reduce memory usage. TcpClient and TcpListener did get some new async methods.

If you want the best scalability, you're probably best using Stephen Toub's custom awaiters for Socket. If you want the easiest to code, you're probably better off using TcpClient and TcpListener.

Do the new Async methods leverage I/O Completion Ports?

Yes, just like most of the other asynchronous APIs in the BCL. AFAIK, the Stream class is the only one that may possibly not use the IOCP; all other *Begin/*End/*Async methods use the IOCP.

Is rolling your own Socket listener more efficient, or are the TcpListener/TcpClient classes pretty good now?

The classes are pretty good as they are. Stephen Toub has a blog post that is a bit more efficient in terms of memory use.

like image 199
Stephen Cleary Avatar answered Oct 26 '22 19:10

Stephen Cleary