Discussion:
write to socket broken pipe (32)
(too old to reply)
c***@hotmail.com
2006-02-14 16:42:58 UTC
Permalink
Hi,

The following function works 99.9% of the time, but every once in a
while, I get a valid socket connection but a failure on the write().
The errno=32 which I believe is a broken pipe. The code used to fail on
every interrupt (EINTR) signal, but from what I've read it seems this
can be expected and one should simply re-attempt the call to write() so
this code reflects that thinking. I've removed the read portion of the
function since it would just clutter things up for debugging purposes.

So what happens is every once in a while I get about 10 "write attempt"
messages followed by the "error on write" message with errno=32.

Any ideas?
Thanks,
Chris

int soapConnect ()
{
int sockfd;
int len, result;
struct sockaddr_in address;
struct hostent *hostinfo;

hostinfo = gethostbyname(ACC_IP);
if (!hostinfo) return(2);
sockfd = socket(AF_INET, SOCK_STREAM,0);
address.sin_family = AF_INET;
address.sin_port = (80);
address.sin_addr = *(struct in_addr *)*hostinfo -> h_addr_list;
len = sizeof(address);

if( (result = connect(sockfd, (struct sockaddr *)&address, len)) < 0 )
{
fprintf(stdout, "SOAP:\t:connect failed");
close(sockfd);
return(3);
}

do {
fprintf(stdout, "SOAP:\twrite attempt");
result = write(sockfd, &soapString, sizeof(soapString));

if( result < 1 && errno != EINTR ) {
fprintf(stdout,"SOAP:\terror on write() errno=%d",errno);
close(sockfd);
return(4);
}

if( errno == EINTR ) {
fprintf(stdout,"SOAP:\t:write interupted...tyring again");
continue;
}

fprintf(stdout, "SOAP:\twrite success");
break;
} while(1);

close(sockfd);
return(1);
}
Nils O. Selåsdal
2006-02-14 17:13:37 UTC
Permalink
Post by c***@hotmail.com
Hi,
The following function works 99.9% of the time, but every once in a
while, I get a valid socket connection but a failure on the write().
The errno=32 which I believe is a broken pipe. The code used to fail on
every interrupt (EINTR) signal, but from what I've read it seems this
can be expected and one should simply re-attempt the call to write() so
this code reflects that thinking. I've removed the read portion of the
function since it would just clutter things up for debugging purposes.
So what happens is every once in a while I get about 10 "write attempt"
messages followed by the "error on write" message with errno=32.
Any ideas?
About what ?
Your code seems fine. EINTR means the call was interrupted by a signal.
It's non fatal, and you should try again.
Provided ofcourse errno 32 is EINTR on your system.

Here errno 32 is EPIPE, you're trying to write to a broken/closed
connection.
c***@hotmail.com
2006-02-14 17:24:16 UTC
Permalink
But why is the errno 32 happening? Is this just to be expected every
once in a while on a busy unix system?

On error 32 would it make sense to close the connection and start over
with a new socket? Or is the error being caused by some flaw in the
code that should be corrected?

Thanks for the reply Nils.
Brian Raiter
2006-02-14 18:58:55 UTC
Permalink
Post by c***@hotmail.com
But why is the errno 32 happening? Is this just to be expected every
once in a while on a busy unix system?
First, find out which error corresponds to errno 32 on your system.
In the future, you should use perror() or strerror() in your code to
get better error messages. For now, one quick way to translate an
errno number into a real error message from the command line is:

$ perl -le '$!=32;print$!'

On my system, this prints "Broken pipe" -- i.e. EPIPE. Your question
makes much less sense if errno 32 is EPIPE on your system as well.

b
c***@hotmail.com
2006-02-14 19:36:51 UTC
Permalink
Hi Brian,

errno 32 is "Broken pipe" on my system (solaris) as well, but why would
this make my question make less sense?

Let me re-phrase.
- is it normal to get broken pipes? if so I shouldn't waste too much
time trying to find the cause , but instead just close the socket and
try again ?

- maybe the pipe is broken on the other end, is there a way to check
this from my end? Maybe more information is available other than
errno=32 ?

Thanks for the help.
Chris
Casper H.S. Dik
2006-02-14 19:41:59 UTC
Permalink
Post by c***@hotmail.com
Let me re-phrase.
- is it normal to get broken pipes? if so I shouldn't waste too much
time trying to find the cause , but instead just close the socket and
try again ?
Yes; if the other end closes the connection then that is what happens.

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
Nils O. Selåsdal
2006-02-15 07:39:18 UTC
Permalink
Post by c***@hotmail.com
Hi Brian,
errno 32 is "Broken pipe" on my system (solaris) as well, but why would
this make my question make less sense?
Let me re-phrase.
- is it normal to get broken pipes? if so I shouldn't waste too much
Sure. You get that when the remote peer dies, and your end eventually
discovers this, and you write to the socket. Or the peer disconnected,
and you didn't detect it, but still writes to it.
c***@hotmail.com
2006-02-15 13:35:26 UTC
Permalink
Post by Nils O. Selåsdal
Post by c***@hotmail.com
Hi Brian,
errno 32 is "Broken pipe" on my system (solaris) as well, but why would
this make my question make less sense?
Let me re-phrase.
- is it normal to get broken pipes? if so I shouldn't waste too much
Sure. You get that when the remote peer dies, and your end eventually
discovers this, and you write to the socket. Or the peer disconnected,
and you didn't detect it, but still writes to it.
Thanks for the help everyone!
David Schwartz
2006-02-23 23:40:57 UTC
Permalink
Post by c***@hotmail.com
Hi Brian,
errno 32 is "Broken pipe" on my system (solaris) as well, but why would
this make my question make less sense?
Let me re-phrase.
- is it normal to get broken pipes? if so I shouldn't waste too much
time trying to find the cause , but instead just close the socket and
try again ?
It's normal. It means that there was some underlying error on the
connection and you aren't doing what you need to do to figure out what that
underlying error is. You may be able to tell what the real error is with
'getsockopt(SO_ERROR)' or by reading instead of writing.

DS

James Antill
2006-02-23 17:32:42 UTC
Permalink
Post by c***@hotmail.com
int soapConnect ()
{
[...]
Post by c***@hotmail.com
result = write(sockfd, &soapString, sizeof(soapString));
Note that you are assuming that write() will either send all of it's data
or none of it ... write() doesn't work like that.
--
James Antill -- ***@and.org
http://www.and.org/and-httpd
Loading...