0
votes

Hello i have a very simple question,

i Have a simple socket server/client program in C

I would like the server side program to keep accepting new connections forever

I want it to be able to accept more connection while it is also performing orther operations

So my question is : what is the best way to do that ?

-should i fork and do accept loop in my child process ?

-should i fork and do accept loop in the parent process, and keep running my operation in the child process ?

Or is there an orther way better than using fork ?

Thank you, bye bye

1
There's quite literally many thousands of tutorials about sockets and network programming in C all over the Internet. Most of them would have parts about how to use multiple connections in parallel, either using polling in a single thread, or using multiple threads, or using multiple distinct processes.Some programmer dude
Another way would be to run an event loop around calls to select() in a single thread, and that thread can do non-blocking I/O operations on both the accepting-socket and all the other sockets that have been returned via accept(). That's how I usually do it; whether it's preferable or not depends a lot on what sort of program you are trying to create (and in particular whether the functionality provided to the various TCP connections need to communicate with each other much, or not)Jeremy Friesner
the most common/efficient method is to generate a 'thread pool' Then as each incoming connection is 'accepted' pass the resulting socket to an idle thread. That thread marks itself as busy, then does all the communication with that individual client, then when all done, closes that socket and marks itself as idleuser3629249

1 Answers

1
votes

The best way is probably using fd_set and select without having to fork every time you accept a new incoming connection. Here's a good explanation for it: https://www.binarytides.com/multiple-socket-connections-fdset-select-linux/