Skip to content
Taiizor edited this page Mar 1, 2023 · 14 revisions

PortExtension Class

The PortExtension class provides extension methods to help with port operations.

Namespace:

Skylark.Standard.Extension.Port

Assembly:

Skylark.Standard.dll

Syntax:

public static class PortExtension

Methods:

Info(int Port = MPPM.Port):

Gets the name of the service listening on the specified Port.

Parameters:

  • Port (int) (Optional) - The port number for which the service name is required. Default value is MPPM.Port.

Return Value:

A string that contains the name of the service, if found; otherwise, the string "Unknown".

Exceptions:

  • Exception - An error occurred while retrieving the service name.

Example:

int port = 80;
string service = port.Info();
Console.WriteLine(service); // Output: "HTTP"

InfoAsync(int Port = MPPM.Port):

Asynchronously gets the name of the service listening on the specified Port.

Parameters:

  • Port (int) (Optional) - The port number for which the service name is required. Default value is MPPM.Port.

Return Value:

A task that represents the asynchronous operation. The task result contains a string that contains the name of the service, if found; otherwise, the string "Unknown".

Exceptions:

  • Exception - An error occurred while retrieving the service name.

Example:

int port = 80;
string service = await port.InfoAsync();
Console.WriteLine(service); // Output: "HTTP"

Info(int[] Ports = null):

Gets the names of the services listening on the specified ports.

Parameters:

  • Ports (int[]) (Optional) - An array of port numbers for which the service names are required. Default value is MPPM.Ports.

Return Value:

A dictionary that contains the names of the services, indexed by their respective port numbers. If a service is not found on a port, the value of the corresponding key is "Unknown".

Exceptions:

  • Exception - An error occurred while retrieving the service names.

Example:

int[] ports = {80, 443};
Dictionary<int, string> services = ports.Info();
foreach (var item in services)
{
    Console.WriteLine("{0}: {1}", item.Key, item.Value);
}
// Output:
// 80: HTTP
// 443: HTTPS

InfoAsync(int[] Ports = null):

Asynchronously gets the names of the services listening on the specified ports.

Parameters:

  • Ports (int[]) (Optional) - An array of port numbers for which the service names are required. Default value is MPPM.Ports.

Return Value:

A task that represents the asynchronous operation. The task result contains a dictionary that contains the names of the services, indexed by their respective port numbers. If a service is not found on a port, the value of the corresponding key is "Unknown".

Exceptions:

  • Exception - An error occurred while retrieving the service names.

Example:

int[] ports = {80, 443};
Dictionary<int, string> services = await ports.InfoAsync();
foreach (var item in services)
{
    Console.WriteLine("{0}: {1}", item.Key, item.Value);
}
// Output:
// 80: HTTP
// 443: HTTPS

PortExtension Class

Namespace: Skylark.Standard.Extension.Port

Assembly: Skylark.Standard.dll

Package: Skylark.Standard

This class provides extension methods for port-related operations.

Inheritance

  • System.Object

Methods

Info

Gets information about a specified port.

Syntax

public static string Info(int Port = MPPM.Port);

Parameters

  • Port (optional)
    The port number to get information about. Defaults to MPPM.Port.

Returns

  • A object that contains information about the specified port.

Examples

The following example demonstrates how to use the Info method to get information about a port.

string result = PortExtension.Info(80);

InfoAsync

Asynchronously gets information about a specified port.

Syntax

public static Task<string> InfoAsync(int Port = MPPM.Port);

Parameters

  • Port (optional)
    The port number to get information about. Defaults to MPPM.Port.

Returns

  • A object representing the asynchronous operation that returns a object that contains information about the specified port.

Examples

The following example demonstrates how to use the InfoAsync method to get information about a port asynchronously.

Task<string> task = PortExtension.InfoAsync(80);
string result = await task;

Info

Gets information about an array of ports.

Syntax

public static Dictionary<int, string> Info(int[] Ports = null);

Parameters

  • Ports (optional)
    An array of port numbers to get information about. Defaults to MPPM.Ports.

Returns

  • A object that contains the key-value pairs representing the properties and their values of the specified object.

InfoAsync

Retrieves information about network ports.

Parameters

  • Ports: An integer array of port numbers. Optional parameter with default value of null.

Returns

A Task<Dictionary<int, string>> object that represents the asynchronous operation. The dictionary contains port numbers as keys and their corresponding information as values.

Scan

Scans a single port on a remote host to check if it is open or closed.

Parameters

  • Address: A string representing the IP address of the remote host. Optional parameter with default value of PortManage.Address.
  • Port: An integer representing the port number to scan. Optional parameter with default value of PortManage.Port.
  • Timeout: An integer representing the timeout value in milliseconds. Optional parameter with default value of PortManage.Timeout.

Returns

A PortType enum value that represents the status of the scanned port (PortType.Open for open and PortType.Close for closed).

ScanAsync

Scans a single port on a remote host to check if it is open or closed asynchronously.

Parameters

  • Address: A string representing the IP address of the remote host. Optional parameter with default value of PortManage.Address.
  • Port: An integer representing the port number to scan. Optional parameter with default value of PortManage.Port.
  • Timeout: An integer representing the timeout value in milliseconds. Optional parameter with default value of PortManage.Timeout.

Returns

A Task<PortType> object that represents the asynchronous operation. The PortType enum value represents the status of the scanned port (PortType.Open for open and PortType.Close for closed).

ScanMultiple

Scans multiple ports on a remote host to check if they are open or closed.

Parameters

  • Address: A string representing the IP address of the remote host. Optional parameter with default value of PortManage.Address.
  • Ports: An integer array representing the port numbers to scan. Optional parameter with default value of null, which means all ports defined in PortManage.Ports will be scanned.
  • Timeout: An integer representing the timeout in milliseconds. Optional parameter with default value of PortManage.Timeout.

The Scan method returns a value of type PortType which indicates whether the specified port is open or closed. The ScanMultiple method returns a Dictionary with keys representing the port numbers and values representing the port types.

The InfoAsync and ScanAsync methods are asynchronous versions of the Info and Scan methods, respectively. They return Task objects that can be awaited.

Example usage:

Dictionary<int, PortType> result = PortExtension.ScanMultiple("127.0.0.1", new int[] { 80, 443, 8080 });
foreach (KeyValuePair<int, PortType> pair in result)
{
    Console.WriteLine("Port {0}: {1}", pair.Key, pair.Value);
}

This code scans ports 80, 443, and 8080 on the local machine and prints the results.

Clone this wiki locally