简体   繁体   中英

Multi threading in custom socket server

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

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

The best resource I've seen on this question is here: http://www.kegel.com/c10k.html

As you can see, people have been wrestling with this issue for decades. It generally comes down to a balance between performance and complexity: spawning a new thread for each client will keep your code very simple but will waste lots of resources, while non-blocking IO will keep resource usage to a minimum but will make your code significantly more complex.

If you can provide some specific numbers (number of client, data transfer rates) I could help estimate which would be the best solution for your use case.

Another important question is what platform(s) your code will run on. Linux now offers edge-triggered non-blocking IO options like epoll that are significantly more performant than their predecessors, but other platforms (Windows, iOS) only have archaic POSIX solutions (select, poll).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM