Skip to content

Commit

Permalink
Update GetProcessByUsedTcpPort (Fix #591)
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 23, 2021
1 parent 8e545fc commit 8269948
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,17 @@ public static void TryReleaseTcpPort(ushort port, string portName)
{
foreach (var p in PortHelper.GetProcessByUsedTcpPort(port))
{
if (p.MainModule!.FileName.StartsWith(Global.NetchDir))
try
{
_ = p.MainModule!.FileName;
}
catch (Exception e)
{
Logging.Warning(e.ToString());
continue;
}

if (p.MainModule.FileName.StartsWith(Global.NetchDir))
{
p.Kill();
p.WaitForExit();
Expand Down
2 changes: 1 addition & 1 deletion Netch/Utils/PortHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static IEnumerable<Process> GetProcessByUsedTcpPort(ushort port)
if (port == 0)
throw new ArgumentOutOfRangeException();

var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port);
var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port).Where(r => r.dwOwningPid is not (0 or 4));

return row.Select(r => Process.GetProcessById((int) r.dwOwningPid));
}
Expand Down

0 comments on commit 8269948

Please sign in to comment.