Search code examples
c++linuxposixbsd

How to abort 'close' a TCP connection?


One normally closes a socket by calling close(). But what if you want to reset / abort the connection (RST packet)? How do you send it from userland?


Solution

  • If I recall, you set the SO_LINGER option.

    struct linger l;
    l.l_onoff = 1;
    l.l_linger = 0;
    
    setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l));