I am trying to debug a scenario, and for that I want the http server to close the connection via RST. Right now it is doing a graceful close with fin/ack.

Is there any way I can manually send a RST packet to close the connection as part of the current stream? may be a simple custom server?

thanks in advance for your help.

有帮助吗?

解决方案

Assuming it is your own code, call setsockopt() with option=SO_LINGER and the linger structure set to l_onoff=1 and l_linger=0, and then close the socket. Any outbound data that is still buffered will be lost, which includes data already sent but not acknowledged.

Use this only for testing. It is insecure and unkind.

If it isn't your code in the server, write a client that does a GET of a large resource and closes the connection without reading any of the response.

Source: W.R. Stevens et al., Unix Network Programming, vol 1, 3rd edition, p.202.

其他提示

For reference, code snippet for the setsockopt() with linger. Thanks to @EJB for the help. With this option set, the server closes the connection with RST.

...
     struct linger so_linger;

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("ERROR opening socket");

     so_linger.l_onoff = 1;
     so_linger.l_linger = 0;
     setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger));
...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top