recv timeout

Steve Holle sholle at link-comm.com
Mon Sep 27 14:21:28 UTC 2004


The following snip was lifted from and excellent tutorial titled "Beej's 
Guide to Network Programming Using Internet Sockets" by Brian "Beej" Hall 
and can be downloaded in many formats at :

http://www.ecst.csuchico.edu/~beej/guide/net/

Short but very useful.

         int RecvTimeout ( int s, char *buf, int len, int timeout )
         {
                 fd_set fds ;
                 int n ;
                 struct timeval tv ;

                 // Set up the file descriptor set.
                 FD_ZERO(&fds) ;
                 FD_SET(s, &fds) ;

                 // Set up the struct timeval for the timeout.
                 tv.tv_sec = timeout ;
                 tv.tv_usec = 0 ;

                 // Wait until timeout or data received.
                 n = select ( s+1, &fds, NULL, NULL, &tv ) ;
                 if ( n == 0  ) return -2 ;                      // Timeout
                 if ( n == -1 ) return -1 ;                      // Error

                 // data must be here, so do a normal recv
                 return recv ( s, buf, len, 0 ) ;
         }

         static void TelnetReadTask (rtems_task_argument fd)
         {
           rtems_unsigned32 count ;

                 char cbuf[512];
                 int n;
                 rtems_status_code sc;
           char buffer [ MAX_RLC_IN_MESSAGE_SIZE+1 ];                // 
plus 1 to leave space for the NULL terminator
           rtems_unsigned32 size ;
           rtems_unsigned32 i ;
           int j;


                 for (;;)
                 {
                         n = RecvTimeout ( fd, cbuf, sizeof(cbuf), 
READ_TIMEOUT_IN_SECONDS ) ;

                         if                      ( n == 0 )
                         {
                                 TelnetConnected = false ;
               telnet_server_ts_cout << "Telnet closed by remote port." << 
std::endl ;
                                 break;
                         }
                         else if ( n == -1 )
                         {
                                 TelnetConnected = false ;
               telnet_server_ts_cout << "Telnet closed by timeout." << 
std::endl ;
                                 break;
                         }
                         else if ( n == -2 )
                         {
                                 TelnetConnected = false ;
               telnet_server_ts_cout << "Telnet closed by error." << 
std::endl ;
                                 break;
                         }
                         else    // Valid data in buffer
                 telnet_server_2_rlc_queue_send ( cbuf, n ) ;
                 }
                 if (close (fd) < 0)
       telnet_server_ts_cout << "Telnet close Error." << std::endl ;
                 sc = rtems_task_delete (RTEMS_SELF);
     telnet_server_ts_cout << "Telnet task delete Error." << std::endl ;
         }


At 01:09 PM 9/26/2004, 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)
>
>

Steve Holle
Link Communications, Inc.
1035 Cerise Rd.
Billings, MT  59101
sholle at link-comm.com  




More information about the users mailing list