Search code examples
socketspoolrecv

How many data can be read when poll return POLLIN


If there is client/server tcp communication case:

The client Send 1MB message to the server, In the server side we use the poll() and recv() function to read the message from the socket, the sequence may like Poll -> POLLIN -> recv -> Poll -> POLLIN - recv ............ Poll -> POLLIN - recv

My question is If the poll() function tell me the socket status is POLLIN, When I read from the socket FD, the size of data read from the socket is return by the recv api as "ssize_t". How much is "ssize_t", is there some rule or some setting we can control. As we don't want to read only 1 byte with 1024*1024 time for 1MB data because it degrade the system performance


Solution

  • POLLIN only signals that some data are available for reading. It does not provide any information how much data are available. But you can simply read as much data you want since recv will not wait until all data you might want are available but will simply return with what can be read so far.