PDA

View Full Version : Putting a timeout on connect and read calls?


SOUR-Monkey
2004.06.29, 12:27 AM
I'm just curious about how to do it. I want to put a timeout on my connect, read and write calls in my socket lib, and I see there are two ways to do it:
Use select to check the status of the socket for the connect call;
or use setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)timeout, sizeof(timeout));

Neither of them I fully understand though, certainly not well enough to implement myself. Could someone give me an overview of how they work? I would think that the second option would simply set the socket to close any read or write calls after the given time-period, but how would that help with the connect call?

As always, any input is appreciated :)

Steven
2004.06.29, 09:33 AM
It's all in setsockopt's manpage ->
SO_SNDTIMEO is an option to set a timeout value for output operations.
It accepts a struct timeval parameter with the number of seconds and
microseconds used to limit waits for output operations to complete. If a
send operation has blocked for this much time, it returns with a partial
count or with the error EWOULDBLOCK if no data were sent. In the current
implementation, this timer is restarted each time additional data are
delivered to the protocol, implying that the limit applies to output por-
tions ranging in size from the low water mark to the high water mark for
output. SO_RCVTIMEO is an option to set a timeout value for input opera-
tions. It accepts a struct timeval parameter with the number of seconds
and microseconds used to limit waits for input operations to complete.
In the current implementation, this timer is restarted each time addi-
tional data are received by the protocol, and thus the limit is in effect
an inactivity timer. If a receive operation has been blocked for this
much time without receiving additional data, it returns with a short
count or with the error EWOULDBLOCK if no data were received. The struct
timeval parameter must represent a positive time interval less than
SHRT_MAX * 10 milliseconds (5 minutes and 28 seconds) otherwise
setsockopt() returns with the error EDOM.

SOUR-Monkey
2004.06.29, 03:15 PM
I had read the man page before I did anything else and I saw the SO_SNDTIMEO flags, but it wasn't clear to me what they actually did. It seems to make a lot more sense now than it did last night though, I must have just been a bit tired :P