有人知道如何通过windows命令行关闭单个连接的TCP或UDP套接字吗?

我在谷歌上搜索了一下,看到一些人也问了同样的问题。但是答案看起来像是netstat或netsh命令的手册页,重点关注如何监视端口。我不想要关于如何监控它们的答案(我已经这样做了)。我想干掉他们。

EDIT, for clarification: Let's say that my server listens TCP port 80. A client makes a connection and port 56789 is allocated for it. Then, I discover that this connection is undesired (e.g. this user is doing bad things, we asked them to stop but the connection didn't get dropped somewhere along the way). Normally, I would add a firewall to do the job, but this would take some time, and I was in an emergency situation. Killing the process that owns the connection is really a bad idea here because this would take down the server (all users would lose functionality when we just want to selectively and temporally drop this one connection).


当前回答

CurrPorts不适合我们,我们只能通过ssh访问服务器,所以也没有TCPView。为了不断开其他连接,我们也不能终止该进程。我们最终做的是在Windows的防火墙上阻止连接,但这还没有被建议。是的,这将阻止所有符合规则的连接,但在我们的情况下,只有一个连接(我们感兴趣的):

netsh advfirewall firewall add rule name="Conn hotfix" dir=out action=block protocol=T
CP remoteip=192.168.38.13

替换为您需要的IP,并根据需要添加其他规则。

其他回答

如果你知道你想要释放的端口,你可以通过寻找特定的端口来排序netstat列表,就像这样:

netstat -ano | findstr :8080

然后pid会出现在右边,你可以用taskkill杀死它。

taskkill /pid 11704 /F

此外,你可能想看看这个问题,这是专门为localhost,但我认为它是相关的:

CurrPorts不适合我们,我们只能通过ssh访问服务器,所以也没有TCPView。为了不断开其他连接,我们也不能终止该进程。我们最终做的是在Windows的防火墙上阻止连接,但这还没有被建议。是的,这将阻止所有符合规则的连接,但在我们的情况下,只有一个连接(我们感兴趣的):

netsh advfirewall firewall add rule name="Conn hotfix" dir=out action=block protocol=T
CP remoteip=192.168.38.13

替换为您需要的IP,并根据需要添加其他规则。

如果你知道你想要删除的特定端口,只需以admin的身份打开命令提示符(在windows上),然后:

NPX kill-port 1900

以上1900是我箱子的端口号。当我想关闭React-Native开发工具(和Expo)正在运行的端口时,我经常使用这个方法。原因是即使关闭了开发人员窗口或停止了服务器,端口仍然在使用中。

为了关闭该端口,您可以确定正在侦听该端口的进程并杀死该进程。

是的,有可能关闭TCP或UDP端口,在DOS中有一个命令

TASKKILL /f /pid 1234 

我希望这对你有用