Reputation: 73
In my program i am using timeval structure of time.h for a TCP/IP socket program, in which the client waits for a timeout value as specified by this structure value the structure initialization is as below
struct timeval tv;
tv.tv_sec = 10;
tv.tv_usec = 0;
and setting socket options as is. Since recv() is a blocking call I've put a timeout:
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval)) ;
and receive data using recv() function. So to verify whether delay is ok I used two variables start & stop of type time_t:
time_t start=clock();
BytesRcvd = recv(sock, CacheBuffer1, sizeof(CacheBuffer1), FLAG);
time_t stop=clock();
time_t difference=difftime(stop,start);
so as per the definitions what I expect is the recv() functions waits for maximum of 10 seconds until data is received via socket. From the server side I didn't send anything. But upon calculating the difference the value I've obtained is 10 but I didn't feel a 10 second delay for reception, but just in the range of milliseconds, so I assume it only took about 10 millisecond
What might be the issue?? Any thoughts?
[update from comment]
My socket is non-blocking that's why I used setsocketopt() function, and I want to wait for a timeout value of 10 Seconds ,ie; if within 10seconds no data is received I have to exit from the recv() function...
Upvotes: 1
Views: 1684
Reputation: 229148
Your time calculation is quite wrong
clock()
calculates CPU time spent on most platforms (Windows is an exception here). Blocking and waiting for data does not consume a lot of CPU time.
Moreover, clock()
returns a clock_t
, not a time_t
, so passing it to difftime()
doesn't make sense. clock()
is also in units of CLOCKS_PER_SEC,so you will need to account for that if you want to convert the time difference to e.g. miliseconds or seconds.
For a start since your timeout is in the order of many seconds, use time() to calculate them time spent, the result will be in seconds. If you need something more granular, use e.g. gettimeofday()
time_t start=time();
BytesRcvd = recv(sock, CacheBuffer1, sizeof(CacheBuffer1), FLAG);
time_t stop=time();
time_t difference=difftime(stop,start);
Upvotes: 0
Reputation: 70951
If your socket is non-blocking recv()
won't block, even if you set a time-out.
Setting a time-out makes sense for blocking sockets, to not have them block for ever.
So if you want the recv()
to block for a certain amount of time, set the socket to be blocking and apply the time-out as you did.
Upvotes: 4