C#如何检测/监控远程连接网络端口的情况(例如:3389端口是否处于监听状态,是否建立了连接等)。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.networkinformation;
namespace test
{
class Program
{
static void Main(string[] args)
{
GetTcpConnections();
}
public static void GetTcpConnections()
{
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpConnection@R_980_4045@ion[] connections = properties.GetActiveTcpConnections();
foreach (TcpConnection@R_980_4045@ion t in connections)
{
Console.Write("Local endpoint: {0} ", t.LocalEndPoint.ToString());
Console.Write("Remote endpoint: {0} ", t.RemoteEndPoint.ToString());
Console.WriteLine("{0}", t.State);
}
Console.WriteLine();
Console.ReadLine();
}
}
}
运行结果:
Local endpoint: 127.0.0.1:1025 Remote endpoint: 127.0.0.1:1026 Established
Local endpoint: 127.0.0.1:1026 Remote endpoint: 127.0.0.1:1025 Established
Local endpoint: 127.0.0.1:1028 Remote endpoint: 127.0.0.1:16992 CloseWait
Local endpoint: 127.0.0.1:1110 Remote endpoint: 127.0.0.1:4900 Established
Local endpoint: 127.0.0.1:2754 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:2762 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:2773 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:2913 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:3014 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:3531 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:4012 Remote endpoint: 127.0.0.1:1110 CloseWait
Local endpoint: 127.0.0.1:4900 Remote endpoint: 127.0.0.1:1110 Established
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。