C - effect of a write() to a closed tcp connection when using signal(SIGPIPE, SIG_IGN);

Go To StackoverFlow.com

0

By closed connection I mean that the other end closed it gracefully. I would expect to receive a 0 from the write() as returned value, but please correct me if I am wrong.

2012-04-03 20:40
by eddy ed


1

Exact behavior may depend on underlying OS but in general you should get an error, i.e., write will return -1 and set errno (to ECONNRESET for instance).

2012-04-03 20:53
by torek
I just tested it on my linux machine, and it returns a value > 0 ! (ie the number of characters it thinks it wrote - eddy ed 2012-04-03 22:12
It's normal to be able to write at least once more; and you can get even more in some cases. On an old BSD system, connecting just to my own local machine, I get one successful write followed by a -1 with errno set to EPIPE. On a more modern Linux box I get the same thing. This is because it takes one extra TCP round-trip for the sender to "see" that the receiver has gone away. Setting SO_KEEPALIVE should cause an earlier discovery but it can take a long time (RFC-1122 says at least 2 hours!) - torek 2012-04-04 07:43
Ads