Re: Socket read() call returns zero on a transient error

From: alok (alokkumargarg_at_gmail.com)
Date: 02/11/05


Date: 11 Feb 2005 12:07:23 -0800

Hi All,
I am also facing the same issue. In case my read call returns zero, i
close the socket connection with the server. While closing the socket ,
i get an error (return value -1) even if i have set the LINGER value to
10 seconds.it shows that even if read returned 0 , there was some data
still left to be read from the socket.
Is there some better way to know that the socket connection at the
other end has been closed.

Here is code snippet:-
   while(done == FALSE)
   {
       rc = read(sock, buf, sizeof (buf));
       if (rc < 0)
       {
          perror("read");
          exit(1);
       }
       if( rc == 0)
       {
          /*No data left to read*/
          done = TRUE;
       }
       else
       {
          strcat(xml_resp,buf,strlen(buf));
          memset(buf,0,1024);
       }
   }
  rc = close(sock);
  if (rc == -1)
  {
     perror("close");
     exit(1);
  }
  return(xml_resp);

Any help will be highly appreciated.

Thanks,
Alok

Keith Shortridge wrote:
> I'm seeing behaviour I didn't expect from a socket and am hoping
> someone can help explain what may be going on.
>
> I have a 'receiver' task running on a machine, reading data from a
number
> of connected sockets using select(). Each of these sockets has been
set into
> non-blocking mode. When select() indicates that a socket is ready for
> reading, I clear errno and I issue a read() call. I then test the
value
> returned by that read() call. If the value is positive, I assume that
I've
> read that many bytes from the socket. If it's negative I look at
errno and
> test for the EINTR or EAGAIN codes. If I have any other error I
report the
> error condition and go into some error handling code. If I have read
data
> or got EINTR or EAGAIN I just return to the select() call waiting for
> more data to come in on one of the monitored sockets. If I get a zero
> value returned by the read() call I assume that this is an end of
file
> condition and that the connection has been closed at the other end.
This
> seems to me to be following Stevens' readn() code in UNIX Network
Programming
> except for the addition of the EAGAIN and EINTR tests. (These are
AF_INET
> stream sockets, I guess I should say.)
>
> This seems to work on ULTRIX, SunOS 4.1, Solaris 2.4 and 2.5, and
OSF/1,
> and has in fact been used for quite a while. It even runs on VMS and
VxWorks.
> I have recently ported the system to HP-UX 10 and have seen
occasional cases
> where the read() call returns zero, with errno still set to zero, but
where
> the connection has not been broken. Indeed, changing the code to
ignore the
> condition and return to the select() call makes it all work. This
seems to
> happen most when the system and/or the network is loaded. It looks
almost
> as if HP-UX has hit some transient error and ought to be returning an
> EAGAIN error, but is in fact returning a zero from read().
>
> Is this actually a problem with HP-UX, or is this a legitimate thing
for
> the read() call to do - even if only HP-UX does it? If it is
> legitimate, how am I supposed to detect a genuine end of file
condition?
> At the moment I have worked around the problem by retrying up to a
> certain number of times when I get a read() returning zero, and
assuming
> I have a transient condition if one of these retries succeeds and
assuming
> I have an end of file if I keep getting the zero return value. This
isn't
> very satisfactory, and if anyone can shed some light on the problem
I'd
> be very grateful.
>
> Keith Shortridge
> Anglo-Australian Observatory
> P.O. Box 296
> Epping, NSW 2121
> Australia
> Tel: +61-2-9372-4822
> Fax: +61-2-9372-4880
> email: ks@aaoepp.aao.gov.au



Relevant Pages

  • Re: sockets -- basic udp client
    ... print "Received from server: %s" % buf ... default for timeout is no timeout, or block forever, your question: ... a time out on the socket, and you receive no data and the timeout ... of detecting a zero length datagram as transmitted ...
    (comp.lang.python)
  • Re: Clarification please
    ... 1)C style string - to check zero in the string as a sentinel ... >> connection, send data, close the socket and repeat. ...
    (microsoft.public.win32.programmer.networks)
  • RE: WSAGetOverlappedResult() returns true when remote client closes the socket
    ... zero then we can assume that the other side has closed the connection. ... WSAGetOverlappedResult doesn't return at all. ... you need to find out if the socket is ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket read() call returns zero on a transient error
    ... In case my read call returns zero, ... close the socket connection with the server. ... Is there some better way to know that the socket connection at the ... > test for the EINTR or EAGAIN codes. ...
    (comp.unix.programmer)
  • Re: Pending WSARecv do not return when closesocket() is called
    ... to fail my pending read on the socket. ... positive value but lpNumberOfBytes is zero. ... Quoting from GetQueuedCompletionStatus: ...
    (microsoft.public.win32.programmer.networks)

Loading