Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 775
0xhacker Avatar asked Nov 26 '12 00:11

0xhacker


People also ask

What are the differences between blocking and non-blocking sockets why would you use one or the other?

In blocking socket mode, a system call event halts the execution until an appropriate reply has been received. In non-blocking sockets, it continues to execute even if the system call has been invoked and deals with its reply appropriately later.

What is socket timeout error?

TCP Socket Timeouts are caused when a TCP socket times out talking to the far end. Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones.

Is socket send blocking?

In case of blocking socket: The send() will block if the kernel buffer is not free enough to intake the data provided to send() call. Non blocking sockets: send() will not block, but would fail and returns -1 or it may return number of bytes copied partially(depending on the buffer space available).

How can you tell if a socket is blocking or non-blocking?

From MSDN, the return value of connect(): On a blocking socket, the return value indicates success or failure of the connection attempt. With a nonblocking socket, the connection attempt cannot be completed immediately. In this case, connect will return SOCKET_ERROR , and WSAGetLastError() will return WSAEWOULDBLOCK.


1 Answers

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() 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.

like image 119
user207421 Avatar answered Sep 28 '22 10:09

user207421