Search code examples
socketsnetwork-programmingsetsockopt

How is it possible to have send timeout on a non blocking socket?


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?


Solution

  • How is it possible to have send timeout on a non blocking socket?

    It isn't. Timeouts are for blocking mode. A non-blocking recv() or send() won't block, and therefore cannot time out either.

    I have seen a lot of code which uses SO_SNDTIMEO.

    Not in non-blocking mode, unless the code concerned is nonsense.

    If you want a timeout in non-blocking mode, that's one of the things the timeout in select() is for.