Broken Pipe problem in Socket Programming

Dear all,

I am making a multithreaded server using socket programming in c .. i'd discovered that when the client closes the connection and the server is still sending him a stream of bytes the server crashed with a BROKEN PIPE problem.
I can easily make the client notify the server before closing and solve the problem. but this is not a practical solution. what if someone implemented my protocol on his own client but did not notify the server ? probably he will crash my server easily.
Also i can easily solve the problem by making each client in a process not in a thread. but this will not be a scalable solution. it will take more memory and slower processing.
how can i solve this problem then ?

Note that i had tested the server on localhost. may this problem be solved if i tested it on a remove server ?

Thanks in advance ..

If you don't want your program to be killed by SIGPIPE, just ignore it.

#include <signal.h>

...

signal(SIGPIPE, SIG_IGN);

Then you'll start getting write() errors instead of being killed.

SIGPIPE is designed for situations like this:

cat reallylongfile | head

head prints the first 10 lines and quits, closing its half of the pipe. With it gone, there's no further reason for cat to continue existing. cat tries to write to the broken pipe, and gets cleanly knocked out by SIGPIPE instead of freaking out with write errors.

It worked !!

Thanks Very Much :slight_smile: