Home > OS >  Multi threading in custom socket server
Multi threading in custom socket server

Time:01-07

I am building a socket server in c.

This server is listening on a port. A huge number of clients will connect to this port.

Each client will stay connected during hours. The clients will sometimes send commands and the server will answer. Sometimes the server will send informations to all the clients. This is why I want the client to stay connected.

I create one thread for each client on the server:

...
while (1) 
{
   client_socket = accept(server_socket, (struct sockaddr*)&s, &size);
   pthread_create(&threads[cpt  ], NULL, reader, &client_socket);
}

It works fine but I think there might be a problem if I have a lot of clients connected at the same time. It will create a huge number of thread and I am wondering if it is a good thing for the performances...

Is there a way to share a thread for multiple clients ?

Thanks

CodePudding user response:

Yes, indeed. Most modern socket servers handle many clients per thread.

To make this work, you need to use non-blocking IO operations and event handling of some sort, so that when work for one client is stalled by input or output, your thread can process work for other clients instead of just waiting.

It's a large topic. There are a lot of different architectures, and the way it's done is typically a little OS-specific. This article looks like a good introduction: https://kaleid-liner.github.io/blog/2019/06/02/epoll-web-server.html

  • Related