Re: Socket crash
Re: Socket crash
- Subject: Re: Socket crash
- From: Ed Watkeys <email@hidden>
- Date: Sat, 6 Dec 2003 14:24:23 -0500
On Dec 6, 2003, at 1:00 PM, Philippe Robin wrote:
Hello,
I am blocked in my developments by the following problem :
I am using the SmallSocket class, I create a listening socket, accept
the connection, send some data,
but when the other end of the socket closes the connection and I am
still sending some data, my app crashes with this message :
Application has exited due to signal 13 (SIGPIPE).
This happens in the SmallSocket code at this line :
sent = send(socketfd, bytes, len, 0);
How do I detect the other en is closed?
How do I trap/avoid this error?
This -- signal handling -- is probably be addressed in Hillegass's
"Core Mac OS X and Unix Programming," or at least it should be. You owe
it to yourself to get a copy of APU: _Advanced Programming in the Unix
Environment_ by W. Richard Stevens and published by Addison Wesley
(maybe now Pearson). Or Hillegass's book, which I've never read but is
highly recommended by some people that I trust.
You need to install a signal handler. For example...
#include <signal.h>
...
static void sig_pipe(int signo) {
if(DEBUG)
printf("Caught SIGPIPE.");
}
---
if(signal(SIGPIPE, sig_pipe) == SIG_ERR)
; /* an error occurred installing signal handler */
...
When you get a broken pipe signal (SIGPIPE), your signal handler will
get called, which will essentially do nothing. Your write operation
will fail and -- if SimpleSocket is written decently -- you will
receive an error that you will be able to handle gracefully.
This sort of behavior is standard, because it's quite common that
there's no point in your program continuing if you can't write to a
file descriptor. (Who checks to see if printf fails?) Here's an
example:
my-prog | more
If I kill more, there's no point in my-prog continuing to run. Most of
the time. For those times when you want your program to continue
running, you need to override the behavior of the default signal
handler.
Keep in mind that I've never done any Cocoa network programming, and I
have no idea how Apple's code deals with these issues, and whether
overriding the signal handler will screw up any of Apple's
socket-handling code.
Ed
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: | |
| >Socket crash (From: Philippe Robin <email@hidden>) |