Socket Programming: Client crashes when exiting server

One of our digital signage applications was crashing when the control server was shutdown at the same time a command was sent with no apparent reason.

It would always crash inside the write function (see code below) and no error message were returned.

sent = write(fd, buf, n);

After a while, we found that in case the connection with the server is lost, and write tries to access the server, it will generate a SIGPIPE signal and in most systems, the application would just exit by default.

A simple way to handle this case was to ignore the pipe signal by adding the following line during initialization:

signal(SIGPIPE, SIG_IGN);

In case the server is down at the time the write function is called, the signal SIGPIPE would be ignored, write would return -1 and errno should return 32 (Broken pipe).

Alternatively, if needs be you could also handle the SIGPIPE signal in your own function.

Share this:

Support CNX Software! Donate via PayPal or cryptocurrencies, become a Patron on Patreon, or buy review samples

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
0 Comments
Advertisement