I’m writing a server that has the ability to receive and send msgs to clients.
I’m using select() to keep track of all connected clients.
select(max_fd+1,&read_fd,&write_fd,NULL,NULL);
I do understand the use of the read_fds in select – if some client sends me data or trying to connect to the server, select will wake up, and the read_fd will contain all the fd’s that I can read data from them.
however, I’m having trouble understanding the use of the write_fds:
I don’t see how and when select will wake up because of a change in write_fd in the server program (and thus I think I don’t need to use it).
I will thank everyone who will clarify the use of write_fd. Thanks!
Very simply, when you send a response to client, there may be a situation when it
does not able yet to receive more data (bandwidth/response time and so on).
In such cases write operation may block.
Thus you need to check for “writability” of the socket before you write into it.
Note if you use nonblocking sockets you should use it only when you receive EAGAIN or EWOULDBLOCK error on write operation, when using blocking sockets you should
always make sure that the socket is writeable (using write_fds) before you send the data.