Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ select async programming

Is there a way to have 'select' waiting for reads and writes, while also being able to add new file descriptors? Preferrably on one thread?

like image 533
poy Avatar asked Nov 21 '25 22:11

poy


1 Answers

Now that I know what your scenario is (a socket-based server that may want to accept new incoming connections), did you know that you can append the file-descriptor for your listening socket to the list for select? See e.g. http://www.lowtek.com/sockets/select.html.

(Paraphrased example:)

fd_set socks;

FD_ZERO(&socks);

// Add listener socket
listen(sock, n);
FD_SET(&socks, sock);

// Add existing socket connections
for (i = 0; i < num_existing_connections; i++)
{
    FD_SET(&socks, connection[i]);
}

// Will break if any of the existing connections are active,
// or if a new connection appears.
select(..., &socks, ...);
like image 105
Oliver Charlesworth Avatar answered Nov 23 '25 13:11

Oliver Charlesworth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!