Re: Posted read() hangs on socket disconnect
Re: Posted read() hangs on socket disconnect
- Subject: Re: Posted read() hangs on socket disconnect
- From: Jens Alfke <email@hidden>
- Date: Wed, 27 May 2009 17:02:01 -0700
On May 27, 2009, at 4:49 PM, Jonathon Kuo wrote: Yes, this seems to be what I'm seeing. I was hoping that there might be some sort of Mac networking framework I could use as an alternate to the low level read() call that would catch this condition?
It depends. If the other computer is the one that's disconnected, you can't detect that perfectly. All you know is you're not getting any packets from it, and that might be because it has nothing to say. The best you can do is have the protocol send no-op data periodically. If you send data and there's no one on the other end, your kernel will disconnect the socket in about 15 sec if it doesn't get an ACK. If the other side is supposed to send data and doesn't, you can use a timer to interrupt your read after the expected ping interval.
If your computer got directly disconnected (Ethernet unplugged or lost WiFi signal) you can use the SystemConfiguration framework to be notified of reachability changes. For example, this is how iChat knows to change your status to disconnected if you lose your network connectivity. The limitation is that this only works if there is a change to a network interface on your computer. If the change is farther away, like your WiFi router having its Ethernet unplugged, or your DSL line being cut, the computer can't tell anything happened, and you're in the same scenario as the previous paragraph.
—Jens |
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden