• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSFileHandle socket question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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/


References: 
 >NSFileHandle socket question (From: Doug Brown <email@hidden>)

  • Prev by Date: loading nib within a nib
  • Next by Date: Re: loading nib within a nib
  • Previous by thread: Re: NSFileHandle socket question
  • Next by thread: Screen grab from a screensaver
  • Index(es):
    • Date
    • Thread