Unix sockets

This is a discussion on Unix sockets within the Linux Programming forums, part of the Platform Specific Boards category; Hi,
I'm experimenting with Unix sockets, and I have a problem when client interrupts connection (with Ctrl-C for example).
Server ...

Unix sockets

Hi,
I'm experimenting with Unix sockets, and I have a problem when client interrupts connection (with Ctrl-C for example).
Server accepts connections and each client is treated in forked process (talk() in the code). On each client's request server responds, and that works until client closes the connection (with close()). Server then knows that client finished because talk() returned and prints message Client quitted. But, if I interrupt the client execution with Ctrl-C, server sometimes (not always, but often) does not return from talk(), i.e. it does not know that client interrupted the connection! Why is that?
If I put usleep(0) after each client's request, then the communication between client and server is slower, but this problem does not occur! Does it mean that client is too fast for the server?
Also, this problem does not happen with Internet sockets. Am I doing something wrong with Unix sockets?
Thanks!

Hm, that could be solution, but question still remains: why Internet server knows that client interrupted and does not have broken pipe, but Unix server cannot figure it out and receives broken pipe? This way, any client can crash my server.

Hm, that could be solution, but question still remains: why Internet server knows that client interrupted and does not have broken pipe, but Unix server cannot figure it out and receives broken pipe? This way, any client can crash my server.

They can only "crash" the server if you don't catch SIGPIPE, which is probably what you should be doing. If a SIGPIPE comes in during a call to recv(), the recv() will return -1 and errno will be set to EINTR. You can then retry the recv() and get a 0 status. You can't just assume it was SIGPIPE, because any signal could have caused the interrupted return.

Also, on the client side, catch SIGINT and shut down gracefully by closing the socket, also it might be a good idea to call shutdown() on it.

Notice you only get a SIGPIPE on an Internet socket when you try to WRITE to it after a RST. But if the server negotiates a shutdown, you get FIN, not RST. Whereas on a UNIX socket there is no such thing, and you get SIGPIPE regardless.