I have a TCP server application in which on receiving an incoming connection, I accept the connection and add the socket descriptor to a list. The server needs the list so it can iterate through the list and send data to all the connected clients periodically.
If any of the clients closes the connection, the corresponding socket descriptor needs to be deleted from the list.
I have tried adding the descriptors to poll() so I can detect any closed connection with may be POLLHUP, but I can’t figure out how to interrupt poll to dynamically add descriptors to its set on every incoming new connection.
The server just needs the list of descriptors of all open connections whenever it wants to send data.
How do I go about this? Any suggestions?
EDIT: Here’s what I did: I created a thread for each client and added its fd to poll.
Pseudo-code on server:
sockfd = accept the conn.
pthread_create
// Inside the thread, add sockfd to poll
struct pollfd fds[1];
fds[0].fd = sockfd;
poll(fds, 1, 500000);
Check for POLLHUP returned event and see which client(fd) closed the connection.
This seems to poll all the descriptors, but my problem now is that when I tested closing the connections with two clients..only one of the descriptors returns POLLHUP.
You should be monitoring the listening socket for activity. Once
pollunblocks the first thing you check is “has my listening socket become interesting?”. If so, accept and add topolland your own list.