我用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 3116 3116 23828
-
Next, you must find the child process ids using the next code
wmic process where (ParentProcessId=23828) get Caption,ProcessId,CommandLine Caption ProcessId wireguard.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
以下是我的操作步骤
netstat -ano|findstr 29999
wmic process get caption,processid,parentprocessid |findstr 31800
wmic process where (ParentProcessId=23828) get Caption,ProcessId,CommandLine
最后执行文章来源:https://www.toymoban.com/news/detail-672994.html
taskkill /f /pid 49412
也就是说c#启动监听服务时,是把任务最终托管给了conhost.exe进程,所以把c#主程序关了,conhost.exe没有结束,端口一直会占用着。以前我一直是重启动系统,现在就不用了:)文章来源地址https://www.toymoban.com/news/detail-672994.html
到了这里,关于c#写的端口监听,程序退出后,再次运行提示端口占用,且进程不存在的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!