Skip to content
Advertisement

C++ + linux handle SIGPIPE signal

Yes, I understand this issue has been discussed many times. And yes, I’ve seen and read these and other discussions:

1 2 3
and I still can’t fix my code myself.
I am writing my own web server. In the next cycle, it listens on a socket, connects each new client and writes it to a vector. Into my class i have this struct:

JavaScript

with next data structures:

JavaScript

and the cycle itself:

JavaScript

it works, clients connect, send and receive requests. But the problem is that if the connection is broken (“^C” for client), then my program will not know about it even at the moment:

JavaScript

as the title of this question suggests, my app receives a SIGPIPE signal. Again, I have seen “solutions”.

JavaScript

but it does not help. At this moment, we have the “№” of the socket to which the write was made, is it possible to “remember” it and close this particular connection in the handler method?
my system:

JavaScript

Advertisement

Answer

As stated in the links you give, the solution is to ignore SIGPIPE, and CHECK THE RETURN VALUE of the write calls. This latter is needed for correct operation (short writes) in all but the most trivial, unloaded cases anyways. Also the fixed write size of 1024 that you are using is probably not what you want — if your response string is shorter, you’ll send a bunch of random garbage along with it. You probably really want something like:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement