Hi,
I want to implement a timeout using SO_RCVTIMEO using setsockopt().
Does anyone have some example on this in C/VC?.
I want to do this for the recv() command.
The receive timeout is 20 secs.
Thanks
Printable View
Hi,
I want to implement a timeout using SO_RCVTIMEO using setsockopt().
Does anyone have some example on this in C/VC?.
I want to do this for the recv() command.
The receive timeout is 20 secs.
Thanks
Thanks,Code:struct timeval tv;
tv.tv_sec = 30; /* 30 Secs Timeout */
setsockopt(sockid, SOL_SOCKET, SO_RCVTIMEO,(struct timeval *)&tv,sizeof(struct timeval));
This works, except "recv" returns after 500ms regardless of the value on my system...
Axl
Maybe you have actually received data.
Mike
PS: Start a new thread. This one's more than two years old
Heh. Initial post was 2005. Then nothing, until 2007. Then nothing, until 2009. Then nothing until yesterday. Well, at least the responses are getting "quicker"! :D
Viggy
first... THANX...
It works for me, but 'tv.tv_sec' seams to be in miliseconds not in seconds. If i put 5000 i got a 5 sec. timeout.
Are you sure the struct timeval should be used as a parameter?
I suppose timeout should be rather passed as int (or unsigned int), in milliseconds:
int nTimeout = 30000; // 30 seconds
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&nTimeout, sizeof(int));
Unfortunately, this is not specified in setsockopt's documentation.
The timing of posts to this thread is hysterical.
WINDOWS:
Timeout value is a DWORD in milliseconds, address passed to setsockopt() is const char *
LINUX:
Timeout value is a struct timeval, address passed to setsockopt() is const void *
vcdeveloper, I think we are close to finding a solution to your issue.
Is it acceptable for a 21 second timeout instead of the 20 seconds you requested?
By various reasons I would like to implement timeout on reading and writing to socket in a server but fail to get it running and therefore kindly ask for some insight into wherein the problem may reside.
In order to set the timeout on the read and write to the socket I'm trying to use of the functions setsocketopt() and getsocketopt(). However I must be doing something wrong as the return value indicates that a problem have occurred and perror outputs "Invalid argument". Strangely enough the error does not always occur at the first usage of setsocketopt() and getsocketopt(), which puzzles me bit.