csocketsasynchronousclient-serverposix-select

C TCP sockets, can 'send' return 0 after using select?


I have a client-server program in C. I'm using select to ensure that the client is ready to accept data, (i.e. client_fd is in write_fds returned by select). If the client is indeed ready to accept data, can I be sure that if I send to this client (using the c method 'send', and number of bytes to be sent is > 0), the number of bytes actually sent (==returned by 'send') is at least 1 byte, or is 0 still possible?


Solution

  • Zero is never possible unless you supply a zero count parameter. It will either transfer at least one byte and return the count, or return -1 with errno == EAGAIN/EWOULDBLOCK if the buffer is full in non-blocking mode, or -1 with some other errno if there is a bad error. It shouldn't give -1/EAGAIN/EWOULDBLOCK if select() has indicated otherwise, except in the presence of multithreaded writing to the socket.