setsockopt and SO_RCV/SNDTIMEO

Gleb Smirnoff glebius at FreeBSD.org
Fri Oct 7 03:31:55 PDT 2005


  Lefteris,

On Fri, Oct 07, 2005 at 01:23:22AM +0300, Lefteris Tsintjelis wrote:
L> I am trying to set something like this up but it doesn't seem to work
L> as I expect it (unless I expect wrong):
L> 
L> timeout.tv_sec = 4;
L> timeout.tv_usec = 0;
L> if(setsockopt(listenSocket, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, 
L> sizeof(timeout)) == -1)
L> 	fprintf(stdout, "setsockopt SO_RCVTIMEO failed: %s\n", 
L> 	strerror(errno));
L> if(setsockopt(listenSocket, SOL_SOCKET, SO_SNDTIMEO, (void *)&timeout, 
L> sizeof(timeout)) == -1)
L> 	fprintf(stdout, "setsockopt SO_SNDTIMEO failed: %s\n", 
L> 	strerror(errno));
L> 
L> When I telnet to the socket, the socket always stays connected. The
L> timer doesn't seem to work right. Am I missing anything here or is
L> this broken?

You are expecting wrong from these socket options. They do not specify
time after which socket should be closed, if no IO is done.

The do limit time the process can block on the socket. So, if you
do read() from this socket and it blocks, it will resume after 4 seconds
even if no input is present.

-- 
Totus tuus, Glebius.
GLEBIUS-RIPN GLEB-RIPE


More information about the freebsd-stable mailing list