I have some problems understanding the working of sockets in Linux.
setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(int));
write = write(sockfd, buf, len);
In the above code as writes are buffered, send timeout doesn't make any sense(write system call will return immediately when the user space buffer is copied into the kernel buffers). Send buffer size is much more important parameter, but send timeout seems it does nothing worthwile. But I am certainly wrong, as I have seen quite a lot of code which uses SO_SNDTIMEO. How can user space code timeout using SO_SNDTIMEO assuming that the receiver is very slow?
SO_SNDTIMEO
on a non-blocking socket is that somewhere in the codebase, the socket gets set back to blocking mode (maybe just temporarily for a particular operation), and the code's author wanted the socket to have a timeout during the time(s) it is set back to blocking mode. (Or, just as likely, the application was originally written to use blocking-mode sockets, and was later converted to use non-blocking sockets instead, and the code's author simply forgot to take out the now-unnecessary SO_SNDTIMEO calls)