开发者

Broken pipe when socket is closed

开发者 https://www.devze.com 2023-02-18 15:52 出处:网络
I have a server/client app on a Linux box. If the server is not up when the client attempts to send a request, I get a SIGPIPE and the application terminates.

I have a server/client app on a Linux box. If the server is not up when the client attempts to send a request, I get a SIGPIPE and the application terminates.

How can I check if the server is available on the socket before I try t开发者_如何学Pythono write?

Also of note, I do not want to trap the SIGPIPE because the client is really part of a shared object that is used by many applications that may or may not already define their own signal handling methods.

Thanks


Pass MSG_NOSIGNAL as flags to send()


This post by kroki describes what seems to be a good method.

To summarize it:

  • Check if SIGPIPE is pending. Record that in a variable. If it is pending before we even start then someone else blocked SIGPIPE. In that case skip all the signal stuff below and just do the write.
  • sigblock SIGPIPE.
  • Do the write.
  • Check if SIGPIPE is pending.
  • If it is pending then sigtimedwait for it with zero timeout.
  • Unblock SIGPIPE.
0

精彩评论

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