[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Read from TCP socket with broken connection.
- Date: 11 Apr 2001 10:59:21 +0400
- From: osv at javad.ru (Sergei Organov)
- Subject: Read from TCP socket with broken connection.
Eric Norum <eric.norum at usask.ca> writes:
> Sergei Organov wrote:
> > When a socket was connected but remote host has closed connection, how
> > subsequent read/write operations should behave? My observation is that read()
> > returns 0 and doesn't set 'errno'. Is it correct behavior?
> > If yes, then how do I distinguish between reading from such a socket and
> > reading from connected socked that results in time-out with zero bytes
> > received? Or, alternatively, how to detect that remote host has closed
> > connection?
> If you've used setsockopt to set a receive timeout and the sender
> doesn't transmit within that time limit the read will return an error
> (-1) with errno set. Otherwise a read will block forever if the remote
> machine terminates without closing down the socket (unless you've turned
> on KEEPALIVE messages).
Thanks, Eric. I'll give it a try.
For whatever reason I thought the behavior should be opposite: return -1 if
connection is broken, and 0 on time-out. Apparently it was my mistake. :-( I
don't have BSD manuals handy and glibc manual doesn't describe timeouts on
sockets at all :-(
Thank you again.
> Eric Norum eric.norum at usask.ca
> Department of Electrical Engineering Phone: (306) 966-5394
> University of Saskatchewan FAX: (306) 966-5407
> Saskatoon, Canada.