C unix sockets. Signals to handle

2019-06-08 17:56发布

问题:

which kind of signal i've to handle in a AF_INET socket, both server and client side?

回答1:

Depending on how you're doing what you're doing, you may have to handle SIG_PIPE, which can happen when the connection is arbitrarily broken.

You should not have to handle any other signals.

If you are using select() or poll() or (personal preference) epoll() you should check for errors (eg, POLLHUP) before you check for read/write availability.

You should also always check the return value of read()/write()/send()/recv(). If there's an error, they return -1, but if they return 0, that means the other end disconnected. Ie, a read of 0 does just mean 0 bytes were read but the connection is still good. It isn't. Close the socket. This is stipulated by POSIX.