Re: recv() hangs (simple question?)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The simple answer (there are more complex answers) is to use select()
with a timeout prior to your blocking socket read.  Some notes to be
aware of:

* The pointer to struct timeval (last arg) will get modified.  You need
to set the struct timeval prior to each call. 

* Here is a code clip:

// m_fd is the socket, and m_tv contains a timeout value
ssize_t do_read( int m_fd, const struct timeval *m_tv )
{ 
  int
    sval;

  ssize_t
    nread = 0;

  ssize_t
    nread = 0;		// bytes read from read()

  struct timeval        // local copy of tv,
    tv = *m_tv;         // select will modify it

  fd_set
    fdsr,
    fdse;

  FD_ZERO( &fdsr );     // and a read set for use with select()
  FD_ZERO( &fdse );     // and an error set for use with select()

  FD_SET( m_fd, &fdsr );  // m_fd is the socket to read, we're
  FD_SET( m_fd, &fdse );  // creating sets for select

  // check for read ready for or socket error
  sval = select( m_fd+1, &fdsr, NULL, &fdse, &tv );

  if( sval > 0 )        // something available to read on m_fd
  {
    nread = read( ... );
  }
  else if( sval == 0 )
  {
    nread = 0;          // nothing happened on m_fd, timed out 
  }
  else                  // select failed
  {
    nread = errno;
    perror( "error" );
  }

  return nread;
}

FWIW,

Nat

On Mon, 2002-07-29 at 16:42, Frank Samuelson wrote:
> 
> I have a client program that performs a recv()
> on a blocking socket.  The program catches SIGPIPEs and exits.
> 
> When the server computer crashes the client hangs on the recv() call.
> It continues to do so even after the server computer comes back up.
> When the server computer shuts down nicely, or the 
> server program is stopped or killed, 
> this is not a problem.  
> 
> Is this standard behavior?  If yes, how to I 
> hack it so the recv() call times out?
> 
> Perhaps I should mention that this connection typically
> carries 6-7 MB/s on a 100Mb connection.  No 
> data in >10 sec is an almost certain sign of a dead server.
> 
> Thanks for any help
> 
> -Frank
> 
> -
> : send the line "unsubscribe linux-net" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-- 
_________________________________________
Nat Ersoz             Myrio Corporation    -o) 
nat.ersoz@myrio.com   Cell: 425-417-5182   /\\
Phone: 425.897.7278   Fax:  425.897.5600  _\_V
3500 Carillon Point   Kirkland, WA 98033 

-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux