I am using poll for implementing the server side of a client-server model with multiplexing.
There's a main loop that the server is running.
In each loop I am checking all the fds at the array of struct pollfd until I find n
sockets that have content for me (where n
is the number that poll returned).
Also, poll is used without timeout (-1 as argument).
So if a new connection is requested at the listening socket I am inserting the socket and some client information into a active connections list I am keeping.
What is the most efficient way to handle the array of poll?
Should I define a small array of size 10 and if there are more clients re allocate memory for an array of size 20 or 50?
Or should I: (a) free the array of struct pollfd type, and (b) reallocate it with size that is equal to the size of the list(+1 for the listening socket) => every time a client closes a connection (and thus I have to remove an element from the array (probably setting the socket to -1, so that poll ignores it) resulting in unused space in array)
What do you recommend?
Thank you for your time
Edit: I think I've found a solution with realloc and shifting with memmove each time a client disconnects to cover his socket at the fds array.