Re: NSFileHandle socket question
Re: NSFileHandle socket question
- Subject: Re: NSFileHandle socket question
- From: Malte Tancred <email@hidden>
- Date: Mon, 24 Dec 2001 12:45:35 +0100
One way to check that the socket is still connected is to call
getpeername() on it. If it's not connected anymore the function
will return -1. To correctly make the error code propagate
correctly to errno you might have to make another call on the
socket, for example try to read() one byte and errno should be
set.
Example:
BOOL socketIsConnected(int socket) {
struct sockaddr_in address;
int dummy;
char character;
if (getpeername(socket, (struct sockaddr *) &address, &dummy) == -1) {
read(socket, &character, 1); // set errno
return NO;
}
return YES;
}
Just a thought...
Cheerio and Happy Christmas,
Malte
Doug Brown:
Hi,
I have been working on creating a socket for an app that I'm working
on, and NSFileHandle seems to be the perfect way to handle sending and
receiving data - it's working just great with
-readInBackgroundAndNotify. I am baffled at one thing, though. How do I
find out if the remote side closed the connection? I don't see any
method or notification that would tell me when the connection is
closed. I need to know this so I can tell the user that the connection
was dropped and reset everything so the app knows it is disconnected
from the server again. Thanks,
Doug
_______________________________________________
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.
--
Malte Tancred
Computer programmer, Oops AB, Sweden
mailto:email@hidden
http://www.oops.se/