【发布时间】:2015-05-28 08:31:09
【问题描述】:
我想计算系统中唯一 IP 地址的数量。
示例:
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.1:xxx
- 127.0.0.1:80 > 1.1.1.2:xxx
- 127.0.0.1:80 > 1.1.1.3:xxx
- 127.0.0.1:80 > 1.1.1.4:xxx
可以计数器显示1.1.1.1(6), 1.1.1.2(1), 1.1.1.3(1), 1.1.1.4(1)
这是我的代码:
谢谢
static void Main(string[] args)
{
do
{
monitor();
System.Threading.Thread.Sleep(5000);
} while (true);
Console.ReadLine();
}
static void monitor()
{
string serverMonitor = "127.0.0.1:80";
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation info in tcpConnections)
{
string list = info.LocalEndPoint.Address.ToString() + ":" + info.LocalEndPoint.Port.ToString();
string remote = info.RemoteEndPoint.Address.ToString() + ":" + info.RemoteEndPoint.Port.ToString();
if (list == serverMonitor)
{
Console.WriteLine(list + " > " + remote + " [" + info.State.ToString() + "]");
}
}
Console.WriteLine("--------------------------------------------------");
}
【问题讨论】:
-
你不知道怎么用
List<string>? -
你好,我是初学者 c#