我用c#写了一个监听29999端口,进程结束后再次启动发现端口被占用,但是运行netstat -ano | findstr 29999找到进程ID后,却没有这个进程
经查询这个监听29999进程虽然没了,但是要找到他的父进程,把父进程关闭了才可以,参考下面的例子
-
In the first place, you must find the process that is causing the issue using the port number:
netstat -abno | findstr /i "listening"| find ":3000"TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 3116
-
secondly, you must find the parent process id (ppid) using code from @Michael
wmic process get processid,parentprocessid | findstr/i 31163116 23828
-
Next, you must find the child process ids using the next code
wmic process where (ParentProcessId=23828) get Caption,ProcessId,CommandLineCaption ProcessIdwireguard.exe 27400
-
Kill all process starting from the child processes to the parent process
taskkill /f /pid 27400 taskkill /f /pid 3116 taskkill /f /pid 23828
以下是我的操作步骤
最后执行
taskkill /f /pid 49412
也就是说c#启动监听服务时,是把任务最终托管给了conhost.exe进程,所以把c#主程序关了,conhost.exe没有结束,端口一直会占用着。以前我一直是重启动系统,现在就不用了:)