Skip to content

Commit

Permalink
Fix #589 GetProcessByUsedTcpPort multiple process
Browse files Browse the repository at this point in the history
  • Loading branch information
chsbuffer committed Mar 22, 2021
1 parent 3f4a31d commit 9a3a1e3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Netch/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public static void PortCheck(ushort port, string portName, PortType portType = P

public static void TryReleaseTcpPort(ushort port, string portName)
{
Process? p;
if ((p = PortHelper.GetProcessByUsedTcpPort(port)) != null)
foreach (var p in PortHelper.GetProcessByUsedTcpPort(port))
{
if (p.MainModule!.FileName.StartsWith(Global.NetchDir))
{
Expand Down
8 changes: 3 additions & 5 deletions Netch/Utils/PortHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ static PortHelper()
}
}

public static Process? GetProcessByUsedTcpPort(ushort port)
public static IEnumerable<Process> GetProcessByUsedTcpPort(ushort port)
{
if (port == 0)
throw new ArgumentOutOfRangeException();

var row = GetTcpTable2().SingleOrDefault(r => ntohs((ushort) r.dwLocalPort) == port);
if (row.dwOwningPid == 0)
return null;
var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port);

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

private static void GetReservedPortRange(PortType portType, ref List<Range> targetList)
Expand Down

0 comments on commit 9a3a1e3

Please sign in to comment.