Reputation: 563
Is there a way to kill off an existing connection??
For example, 192.168.1.120 is connected to be via port 8080.
I would like to know how to terminate that connection?
Many thanks!
Upvotes: 5
Views: 7603
Reputation: 1130
Since this question is tagged C++ (and I had the same question), I presume that the OP wanted an example that uses the Windows API. I've done this using the GetTcpTable2
and SetTcpEntry
APIs. You'll need to call GetTcpTable2
and filter the results to the connection you want to terminate, then pass it to SetTcpEntry
. Note that I took a bit of a shortcut with allocating memory for GetTcpTable2
, so if you're writing production code, you'll want to read the Microsoft documentation on how best to correctly use the API.
Feel free to use the code I wrote and published under the MIT licence on Github:
Upvotes: 3
Reputation: 86506
Easiest way (without external software) is to kill and/or restart the process that's watching that port. That'll kill off every connection used by that process, though, so it's not something you normally want to do on a server.
To find out which process is handling the connection on your side, you can say netstat -anp
in Linux, or netstat -anb
in Windows. Note, in Windows, netstat
can be quite slow in backtracking from a socket to a process.
If you're using Windows, and don't mind downloading something, check out SysInternals' TCPView. Lets you see what ports are open, and what's using them. It can even kill connections, IIRC, though i forget whether it's by simply resetting the connection or by killing the process using it.
Upvotes: 2