Re: Socket read() call returns zero on a transient error
From: alok (alokkumargarg_at_gmail.com)
Date: 02/11/05
- Previous message: alok: "Re: Socket read() call returns zero on a transient error"
- Maybe in reply to: alok: "Re: Socket read() call returns zero on a transient error"
- Next in thread: David Schwartz: "Re: Socket read() call returns zero on a transient error"
- Reply: David Schwartz: "Re: Socket read() call returns zero on a transient error"
- Reply: Villy Kruse: "Re: Socket read() call returns zero on a transient error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Feb 2005 12:10:24 -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
- Previous message: alok: "Re: Socket read() call returns zero on a transient error"
- Maybe in reply to: alok: "Re: Socket read() call returns zero on a transient error"
- Next in thread: David Schwartz: "Re: Socket read() call returns zero on a transient error"
- Reply: David Schwartz: "Re: Socket read() call returns zero on a transient error"
- Reply: Villy Kruse: "Re: Socket read() call returns zero on a transient error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|