I need to write a chat client server for a class using unix sockets (without O_NONBLOCK) and select asynchronous I / O for them. At the moment, on the server, I read 1024 bytes from the client and directly process it.
For example, in the case of a message, I get a command formatted as MSG <msg> (representing the client sending the message), I will go through all the sockets of the connected clients and write a message to them.
This approach does work, but I recently found by reading man send that it can block if the socket buffer is full and the O_NONBLOCK flag is not set on the socket.
I think this problem can occur when the client does not read for some reason (failure, listening, etc.), and this will be crucial for my server, since it will basically block until this client will not be read again.
So here is my question:
What is the correct approach to a potentially blocking socket to avoid sending a block if the socket buffer is full?
I am currently using select only to check if there is something to read on sockets, but maybe I should use it also to find out if I can write on a specific socket too? And also, can I find out how many bytes I can read / write when return is selected? For example, if you select "says" that I can write on this socket, how can I find out how many bytes I can write on the maximum before writing on this socket actually becomes a lock?
Thanks.
source share