× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Mon, 17 Dec 2001 PatrickConner@parkdalemills.com wrote:
>
> How can I set a time-out value for a socket read?
>
> I had a client program that communicates with another box hang up for 30
> minutes last Friday. It was waiting for a response.
>

The select() API lets you ensure that there is data on the socket before
you read it.   You can call select() with a timeout prior to your recv
to ensure that you get data within a timeout period.

Usually, when timeouts are a concern in an application, I'll use a
subprocedure that calls select() right before recv().

Here is an example of such a subproc.  It uses the FD_xxx subprocedures
from my socket utilities service program, if you don't already have your
own versions of these that you can convert it to, you can get mine from
http://klement.dstorm.net/rpg/socktut/

      *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      *  RecvDta:  Read data from a socket with a timeout.
      *
      *    This is identical to the recv() API call, except that
      *    it times out if no data is received in the timeout value.
      *
      *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     P RecvDta         B
     D RecvDta         PI            10I 0
     D   socket                      10I 0 value
     D   data                          *   value
     D   length                      10I 0 value
     D   flags                       10I 0 value
     D   timeout                     10I 0 value

     D set             S                   like(fdset)
     D p_oldtv         S               *
     D size            S             10I 0

     C*********************************************************
     C* Wait for data, up to "timeout" seconds:
     C*********************************************************
     c                   eval      p_oldtv = p_timeval
     c                   eval      size = %size(timeval)
     c                   alloc     size          p_timeval

     c                   callp     FD_ZERO(fdset)
     c                   callp     FD_SET(socket: fdset)

     c                   eval      tv_sec = timeout
     c                   eval      tv_usec = 0

     c                   callp     select(socket+1: %addr(set): *NULL:
     c                                    *NULL: p_timeval)

     c                   dealloc                 p_timeval
     c                   eval      p_timeval = p_oldtv

     c                   if        FD_ISSET(socket: set) = *Off
     c                   return    0
     c                   endif

     C*********************************************************
     C*  Now read the data...
     C*********************************************************
     c                   return    recv(socket: data: length: flags)
     P                 E



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.