Class PortService
- Namespace
- McServerLauncher.Services
- Assembly
- McServerLauncher.dll
Checks which ports are in use on the machine (by any application) and helps find the next free one. TCP and UDP are asked separately because they are separate namespaces.
public class PortService
- Inheritance
-
PortService
- Inherited Members
Methods
FindFreePort(int, ISet<int>)
First free port from start that is not in use by the system nor in
the alsoAvoid set (e.g. ports of other registered servers). Returns
null if every port up to 65535 is taken, so the caller can react instead of silently
getting a busy port back. The system's listener table is snapshotted once for the whole
scan (instead of re-queried per port).
public int? FindFreePort(int start, ISet<int> alsoAvoid)
Parameters
Returns
- int?
FindFreeUdpPort(int, ISet<int>)
First free UDP port from start, skipping alsoAvoid.
Null when everything up to 65535 is taken.
public int? FindFreeUdpPort(int start, ISet<int> alsoAvoid)
Parameters
Returns
- int?
FindFreeUdpPort(int, ISet<int>, out bool)
First free UDP port from start, skipping alsoAvoid.
Null when everything up to 65535 is taken. systemPortsRead says whether
the system's UDP table could actually be read.
public int? FindFreeUdpPort(int start, ISet<int> alsoAvoid, out bool systemPortsRead)
Parameters
Returns
- int?
Remarks
The flag exists because the two answers used to be indistinguishable. When the table cannot
be queried every port looks free, so the search returns start — the same
value it returns when it has genuinely checked and the port is available. A caller handing
that port to Geyser could not tell "19132 is free" from "I have no idea what is bound", and
the difference only surfaced later as a Geyser that silently failed to bind.
GetListeningPid(int)
PID of the process listening (LISTEN) on that TCP port, or null.
public int? GetListeningPid(int port)
Parameters
portint
Returns
- int?
IsPortInUse(int)
True if any system process is listening on that TCP port.
public bool IsPortInUse(int port)
Parameters
portint
Returns
IsUdpPortInUse(int)
True if any system process is listening on that UDP port.
public bool IsUdpPortInUse(int port)
Parameters
portint