c#写的端口监听,程序退出后,再次运行提示端口占用,且进程不存在

文章讲述了作者在使用C#编写监听29999端口的应用时遇到的问题,进程结束后端口仍被占用。通过netstat和WMIC命令追踪到相关的父进程和子进程,最终发现是conhost.exe进程导致的。解决方法是找到并结束所有相关子进程,避免重启系统。

我用c#写了一个监听29999端口,进程结束后再次启动发现端口被占用,但是运行netstat -ano | findstr 29999找到进程ID后,却没有这个进程

 经查询这个监听29999进程虽然没了,但是要找到他的父进程,把父进程关闭了才可以,参考下面的例子

  1. 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
    
  2. secondly, you must find the parent process id (ppid) using code from @Michael

    wmic process get processid,parentprocessid | findstr/i 3116
        3116             23828
    
  3. 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
    
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值