PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.PortFinder Class Reference

Utility class to find open ports for binding to. More...

Static Public Member Functions

static int FindOpenPort ()
 Attempts to find a random unbound port on the loopback device (localhost) within the range 1024-65535.
static int FindOpenPort (IPAddress forAddress)
 Attempts to find an unbound port on the provided ip address within the range 1024-65535.
static int FindOpenPort (IPAddress forAddress, int min, int max)
 Attempts to find an unbound port on the provided ip address within the provided range.
static int FindOpenPort (IPAddress forAddress, int min, int max, Action< string > attemptLogger)
 Attempts to find an unbound port on the provided ip address within the provided range.
static void ResetUsedHistory ()
 PortFinder will store ports which have been handed out before so as to avoid race conditions between contenders. Ordinarily, this shouldn't be a problem - there are 64k ports to choose from - but if you are sure you don't mind a conflict and have the need to, you can reset the used history here.
static int FindOpenPort (IPAddress forAddress, int min, int max, Func< int, int, int, int > portAttemptGenerator)
 Attempts to find an unbound port on the provided ip address within the provided range using the provided port attempt generator The portAttemptGenerator callback is called with 3 arguments:
static int FindOpenPortFrom (int start)
 Attempt to find an open port on the ipv4 loopback interface (localhost / 127.0.0.1), starting at the provided value and sequentially testing ports in order, until one can (probably) be used.
static int FindOpenPortFrom (IPAddress forAddress, int start)
 Attempt to find an open port, starting at the provided value and sequentially testing ports in order, until one can (probably) be used.
static int FindOpenPort (IPAddress forAddress, int min, int max, Func< int, int, int, int > portAttemptGenerator, Action< string > attemptLogger)
 Attempts to find an unbound port on the provided ip address within the provided range using the provided port attempt generator The portAttemptGenerator callback is called with 3 arguments:
static bool PortIsActivelyInUse (int port)
 Tests if the provided port is currently bound on the loopback device (localhost)
static bool PortIsActivelyInUse (IPAddress forAddress, int port)
 Tests if the provided port is currently bound on the provided device.

Static Public Attributes

const int MIN_PORT = 1024
 The lowest port number that is considered by default.
const int MAX_PORT = 32767
 The highest port number considered by default.

Detailed Description

Utility class to find open ports for binding to.

Member Function Documentation

◆ FindOpenPort() [1/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( )
static

Attempts to find a random unbound port on the loopback device (localhost) within the range 1024-65535.

Returns

◆ FindOpenPort() [2/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( IPAddress forAddress)
static

Attempts to find an unbound port on the provided ip address within the range 1024-65535.

Parameters
forAddress
Returns

◆ FindOpenPort() [3/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( IPAddress forAddress,
int min,
int max )
static

Attempts to find an unbound port on the provided ip address within the provided range.

Parameters
forAddress
min
max
Returns

◆ FindOpenPort() [4/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( IPAddress forAddress,
int min,
int max,
Action< string > attemptLogger )
static

Attempts to find an unbound port on the provided ip address within the provided range.

Parameters
forAddress
min
max
attemptLoggerAction to invoke to log port finder attempts
Returns

◆ FindOpenPort() [5/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( IPAddress forAddress,
int min,
int max,
Func< int, int, int, int > portAttemptGenerator )
static

Attempts to find an unbound port on the provided ip address within the provided range using the provided port attempt generator The portAttemptGenerator callback is called with 3 arguments:

  • minimum port
  • maximum port
  • last attempt and should return a new value to test
Parameters
forAddress
min
max
portAttemptGenerator
Returns

◆ FindOpenPort() [6/6]

int PeanutButter.Utils.PortFinder.FindOpenPort ( IPAddress forAddress,
int min,
int max,
Func< int, int, int, int > portAttemptGenerator,
Action< string > attemptLogger )
static

Attempts to find an unbound port on the provided ip address within the provided range using the provided port attempt generator The portAttemptGenerator callback is called with 3 arguments:

  • minimum port
  • maximum port
  • last attempt and should return a new value to test
Parameters
forAddress
min
max
portAttemptGenerator
attemptLoggerAction to invoke to log port finder attempts
Returns

◆ FindOpenPortFrom() [1/2]

int PeanutButter.Utils.PortFinder.FindOpenPortFrom ( int start)
static

Attempt to find an open port on the ipv4 loopback interface (localhost / 127.0.0.1), starting at the provided value and sequentially testing ports in order, until one can (probably) be used.

Parameters
start
Returns

◆ FindOpenPortFrom() [2/2]

int PeanutButter.Utils.PortFinder.FindOpenPortFrom ( IPAddress forAddress,
int start )
static

Attempt to find an open port, starting at the provided value and sequentially testing ports in order, until one can (probably) be used.

Parameters
forAddress
start
Returns

◆ PortIsActivelyInUse() [1/2]

bool PeanutButter.Utils.PortFinder.PortIsActivelyInUse ( int port)
static

Tests if the provided port is currently bound on the loopback device (localhost)

Parameters
port
Returns

◆ PortIsActivelyInUse() [2/2]

bool PeanutButter.Utils.PortFinder.PortIsActivelyInUse ( IPAddress forAddress,
int port )
static

Tests if the provided port is currently bound on the provided device.

Parameters
forAddress
port
Returns

Member Data Documentation

◆ MAX_PORT

const int PeanutButter.Utils.PortFinder.MAX_PORT = 32767
static

The highest port number considered by default.

◆ MIN_PORT

const int PeanutButter.Utils.PortFinder.MIN_PORT = 1024
static

The lowest port number that is considered by default.

  • 1024 because that doesn't require privilege escalation

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.Utils/PortFinder.cs