- . . kqueue (FreeBSD) epoll (Linux), - (AIO)
So, if select () is a choice, you can use the following approach (just an example, something like pseudocode):
fd_set read_set, write_set;
struct timeval timeout;
while(!quit)
{
// adds your sockets to fd_set structure, return max socket + 1, this is important!
max = fillFDSet(&read_set);
setReadTimeout(&timeout); // sets timeout for select
if (0 < select(max, &read_set, NULL, NULL, &timeout)) // wait for read
{
// there is at least one descriptor ready
if (FD_ISSET(your_socket))
{
socket_size = read(socket, socket_buffer, 1024);
}
}
max = fillFDSet(&write_set);
setWriteTimeout(&timeout); // sets timeout for select
if (0 < select(max, NULL, &write_set, NULL, &timeout)) // wait for write
{
// there is at least one descriptor ready
if (FD_ISSET(your_socket))
{
write(socket, socket_buffer, socket_size);
}
}
}
source
share