recv timeout

Angelo Fraietta afraiett at bigpond.net.au
Mon Sep 27 02:34:41 UTC 2004


Thanks - I'll give it a go

Ian Caddy wrote:

> 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?.
>>
>

-- 
Angelo Fraietta

PO Box 859
Hamilton NSW 2303

Home Page


http://www.users.bigpond.com/angelo_f/

There are those who seek knowledge for the sake of knowledge - that is CURIOSITY
There are those who seek knowledge to be known by others - that is VANITY
There are those who seek knowledge in order to serve - that is LOVE
    Bernard of Clairvaux (1090 - 1153)






More information about the users mailing list