Concurrent TCP servers
The basic idea 1 client = 1 task. The task is alive as long until the connection is closed The task closes the connection
Why make servers concurrent? Concurrency One server can service several clients at the same time. One client does not have to wait for another client to finish… No waiting for I/O Some servers (like HTTP and file servers) read files. Reading a file takes a lot of time (compared to other operations) Robustness 1 One buggy/evil (or just slow) client cannot “take over” the server Example: The client makes a connection, but never sends a request Robustness 2 An error in the handling of a client, does not affect the handling of other clients.
Concurrent TCP servers: C# task Server = New TcpListener(host, port) Server.Start(); While (true) Client = server.AcceptTcpClient(); Task.Run(() => DoIt(client)); DoIt(client) gives service to this client.
Concurrent TCP servers: C# helper class Sometimes Task.Run(…) is to simple … Make a helper class to handle the request DoIt() is implemented in the helper class Parameters are handed to the helper class through its constructor Server = New TcpListener(host, port) Server.Start(); While (true) Client = server.AcceptTcpClient(); Helper helper = new Helper(client, other parameters); Task.Run(() => helper.DoIt());