-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortChecker.cs
More file actions
72 lines (62 loc) · 1.84 KB
/
PortChecker.cs
File metadata and controls
72 lines (62 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace NodaStack
{
public static class PortChecker
{
public static bool IsPortAvailable(int port)
{
try
{
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
var tcpConnInfoArray = ipGlobalProperties.GetActiveTcpListeners();
foreach (var tcpInfo in tcpConnInfoArray)
{
if (tcpInfo.Port == port)
return false;
}
return true;
}
catch
{
return false;
}
}
public static async Task<Dictionary<int, bool>> CheckPortsAsync(IEnumerable<int> ports)
{
var results = new Dictionary<int, bool>();
await Task.Run(() =>
{
foreach (var port in ports)
{
results[port] = IsPortAvailable(port);
}
});
return results;
}
public static List<int> GetAvailablePorts(int startPort = 8000, int endPort = 9000)
{
var availablePorts = new List<int>();
for (int port = startPort; port <= endPort; port++)
{
if (IsPortAvailable(port))
{
availablePorts.Add(port);
}
}
return availablePorts;
}
public static Dictionary<string, int> GetServicePorts()
{
return new Dictionary<string, int>
{
{ "Apache", 8080 },
{ "PHP", 8000 },
{ "MySQL", 3306 },
{ "phpMyAdmin", 8081 }
};
}
}
}