Socket Programming: Client crashes when exiting server

Orange Pi Development Boards

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.

Leave a Reply

avatar
  Subscribe  
Notify of