Broken pipe when the outlet is closed

I have a server / client application in a Linux box. If the server is down when the client tries to send a request, I get SIGPIPE and the application terminates.

How to check if a server is accessible on a socket before trying to write?

It should also be noted that I do not want to catch SIGPIPE, because the client is indeed part of a common object that is used by many applications that may or may not already define their own signal processing methods.

thanks

+7
source share
2 answers

Pass MSG_NOSIGNAL as flags to send()

+6
source

This post from kroki describes what seems like a good method.

To summarize this:

  • Check if SIGPIPE is waiting. Record that in a variable. If it is not already running, we just block SIGPIPE. In this case, skip all the data below and just write.
  • sigblock SIGPIPE.
  • Take a note.
  • Check if SIGPIPE is waiting.
  • If he expects, then sigtimedwait for him with a zero timeout.
  • Unlock SIGPIPE.
0
source

All Articles