recv timeout
Ian Caddy
ianc at microsol.iinet.net.au
Mon Sep 27 02:23:43 UTC 2004
We use the setsockopt as described by Eric.
Here is an example function that we use in our system:
int SetSocketTimeout(int connectSocket, int milliseconds)
{
struct timeval tv;
tv.tv_sec = milliseconds / 1000 ;
tv.tv_usec = ( milliseconds % 1000) * 1000 ;
return setsockopt (connectSocket, SOL_SOCKET, SO_RCVTIMEO, (char
*)&tv, sizeof tv);
}
Then when you are waiting to receive a packet, use the following function:
iRc = recv(connectSocket, pTmpRxBuffer, u16MessageDataLen, 0 );
if (iRc == 0)
{
/* Socket has been disconnected */
printf("\nSocket EOF\n");
close(connectSocket);
connectSocket = 0;
return u32Rc;
}
if(iRc < 0)
{
printf("\nSocket recv returned %d, errno %d\n",iRc,errno);
close(connectSocket); /* Close socket if we get an error */
connectSocket = 0;
}
If iRc is positive, it gives you the number of chars received.
I hope this helps.
regards,
Ian Caddy
Angelo Fraietta wrote:
>
> Eric Norum wrote:
>
>>
>> On Sep 24, 2004, at 11:57 AM, Steve Holle wrote:
>>
>>> I have a simple socket application and I would like to timeout recv
>>> after a fixed period of inactivity. I found an article showing how
>>> to do it using select but I am under the impression this is not
>>> supported.
>>>
>>> What would be the best way to accomplish the timeout.
>>
>>
>>
>> select() works with sockets, but is a very expensive call. A better
>> technique is to use the SO_RCVTIMEO socket option.
>>
>
> Do you have an example or code snippet using it?.
>
More information about the users
mailing list