views:

48

answers:

1

I have some code that seems to not handle it well when a TCP connection is closed via the RST flag instead of a normal handshake for closing the connection. The "connection reset by peer" situation. I'd like to write a TCP server that always closes via RST so that I can reproduce the bug and write some unit tests for this. So...

How do I send a RST instead of a normal close, for testing?

+5  A: 

You can get a RST by modifying your SO_LINGER setting. You want l_onoff set to non-zero and l_linger set to zero. Once you do that, closing the socket will cause a reset.

struct linger lng;
lng.l_onoff = 1;
lng.l_linger = 0;
setsockopt(sock, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng));
close(sock);
R Samuel Klatchko
Some explanation: http://www.developerweb.net/forum/archive/index.php/t-2982.html
Nikolai N Fetissov
Thanks, and FWIW the Java equivalent is `socket.setSoLinger(true, 0);`
Harold L