简体   繁体   中英

Multiple Socket Connections

I need to write a server which accepts connections from multiple client machines, maintains track of connected clients and sends individual clients data as necessary. Sometimes, all clients may be contacted at once with the same message, other times, it may be one individual client or a group of clients.

Since I need confirmation that the clients received the information and don't want to build an ACK structure for a UDP connection, I decided to use a TCP streaming method. However, I've been struggling to understand how to maintain multiple connections and keep them idle.

I seem to have three options. Use a fork for each incoming connection to create a separate child process, use pthread_create to create an entire new thread for each process, or use select() to wait on all open socket IDs for a connection.

Recommendations as to how to attack this? I've begun working with pthreads but since performance will likely not be an issue, multicore processing is not necessary and perhaps there is a simpler way.

Child processes are not nice, because you just move the goalpost. You will need to make your child processes communicate between each other, then you are back to the same problem.

It is possible to use threads, but you will have other problems if your threads keep blocking on socket receive.

select() (or poll() on newer (POSIX) Unixes) is still the best solution. You tell either select() or poll() which sockets or descriptors you want to monitor for events (probably just input (read) events is enough for you), then you do the read only on that socket or descriptor that was flagged by select()/poll(). It is guaranteed that recv() will not block.

Read the C10K page for whole bunch of options. Then read the High Performance Server Architecture article. These will answer lots of questions for you.

I would go with the third option. For that look into epoll(4) and/or kqueue(2) facilities for modern performant select / poll replacements.

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