I saw some code examples to explain select() can handle multiple events at the same time as below:
FD_SET readfds;
FD_SET writefds;
FD_SET exceptfds;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(someRandomSocket, &readfds);
FD_SET(someRandomSocket, &writefds);
FD_SET(someRandomSocket, &exceptfds);
timeval timeout;
timeout.tv_sec = ...;
timeout.tv_usec = ...;
int total = select(0, &readfds, &writefds, &exceptfds, &timeout);
if (total == -1)
{
// error in select() itself, handle as needed...
}
else if (total == 0)
{
// timeout, handle as needed...
}
else
{
if (FD_ISSET(someRandomSocket, &exceptfds) )
{
// socket has inbound OOB data, or non-blocking connect() has failed, or some other socket error, handle as needed...
}
if (FD_ISSET(someRandomSocket, &readfds) )
{
// socket has inbound data, or has disconnected gracefully, handle as needed...
}
if (FD_ISSET(someRandomSocket, &writefds) )
{
// socket is writable, or non-blocking connect() has succeeded, handle as needed...
}
}
However, I am wondering how about if the socket has write data and read data at the same time. How can I only get the read data or the write data from the socket?
For example, if FD_ISSET(someRandomSocket, &readfds) and FD_ISSET(someRandomSocket, &writefds) are both true at the same time, if I just want to get the read data, is it possible to also get the read data when I use recv(someRandomSocket, buf, 8192, 0)? Or if I just want to send out the write data, is it possible to also send out the write data when I use send(someRandomSocket, buf, 8192, 0)?