开发者

How to detect TCP connection lost?

开发者 https://www.devze.com 2023-02-06 17:51 出处:网络
I have crossplatform code I\'ve written at the socket level on Linux and iPhone. It opens nonblocking sockets and connects to a server. Everything works great except sometimes when one of the client/s

I have crossplatform code I've written at the socket level on Linux and iPhone. It opens nonblocking sockets and connects to a server. Everything works great except sometimes when one of the client/server disconnects it halts exec via SIGPIPE on the other during a recv or send call.

That makes sense. On Linux I can use a flag to indicate to the OS lib not to raise SIGPIPE - then I can sense the error myself and handle it cleanly. That works fine

However on iPhone OS (and presumably osx which I later care about) there's no flag 开发者_StackOverflowto indicate not to throw SIGPIPE. I can do signal(SIGPIPE, IGN); which seems to USUALLY stop it, but still sometimes I get it??? (This could be due to where I set that signal handling up - I may only do it on app start and I instead may have to do it every time the app becomes active??)

This all makes me feel like I'm catching the disconnect the wrong way - should I be using select() or friends to sense if a disconnect has occurred?? Specifically how can I do that?


You really should've googled for that one (for example, this page). In short, try using SO_NOPIPE socket option in addition to what you've already tried.

0

精彩评论

暂无评论...
验证码 换一张
取 消