C#检测远程计算机端口连通性:TcpConnectionInformation
在使用C#进行网络通信开发时,有时需要验证目标服务器上的特定端口是否已开放并接受连接。虽然可以使用Socket尝试建立连接,但更轻量且直接的方法是利用 .NET Framework 提供的网络接口枚举能力,通过分析当前系统的活动TCP连接表来实现。
实现原理
核心思路是查询系统当前的“传输层”状态。Windows操作系统维护着一个活动TCP连接表,我们可以通过 System.Net.NetworkInformation 命名空间下的 API 获取这些数据。如果目标端口在列表中呈现为 Established(已建立)或其他活跃状态,则说明端口是开放的。
准备工作
- 运行环境: .NET Framework 2.0 及以上版本(适用于 Windows)。
- 权限要求: 读取网络连接状态通常不需要管理员权限,但在某些受限网络环境下可能受限。
- 依赖库: System.dll 中的 System.Net.NetworkInformation 命名空间。
操作步骤
第1步:获取全局网络属性对象
目的是获取当前主机关于 TCP/UDP/IP 的全局配置信息。入口为静态工厂方法。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;
namespace PortChecker
{
public class Program
{
// ...
第2步:检索活跃的连接列表
调用 GetActiveTcpConnections() 方法,这将返回一个包含系统中所有活跃 TCP 连接的数组。这是执行端口状态判断的数据源。
public static void GetTcpConnections()
{
// 获取全局网络属性
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
// 获取所有的活跃TCP连接
TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
// ...后续处理
第3步:遍历并过滤目标端口
为了检测特定远程端口(例如常见的 RDP 3389 端口或自定义服务端口),我们需要遍历上一步获取的数组。重点检查每个连接的 State 是否为 TcpState.Established(已建立),并比对 RemoteEndPoint.Port 是否匹配目标端口号。
foreach (TcpConnectionInformation t in connections)
{
// 打印或判断逻辑
Console.Write("Local endpoint: {0} ", t.LocalEndPoint.ToString());
Console.Write("Remote endpoint: {0} ", t.RemoteEndPoint.ToString());
Console.WriteLine("State: {0}", t.State);
// 示例:如果想专门查找状态为 Established 的特定端口连接
// if (t.RemoteEndPoint.Port == 3389 && t.State == TcpState.Established)
// {
// Console.WriteLine("Target port is open and connected.");
// }
}
第4步:运行与结果解读
编译并运行上述代码,控制台将输出所有活动的 TCP 连接详情。每一行代表一个连接实例,包含本地端点、远程端点以及关键的状态字段。
典型的输出结果如下所示,其中 Established 表示连接正常开启,CloseWait 表示对端已关闭连接但本方尚未关闭:
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
补充说明
- 局限性:
GetActiveTcpConnections仅能检测已经建立的 TCP 连接。如果端口开放但没有任何程序当前与该端口进行数据传输(即无活跃连接),此方法可能会返回空列表或无法发现该端口处于监听状态。
对于纯粹的“端口是否存在监听”检测,建议使用 Socket 尝试连接或使用netstat命令行工具解析。 - 跨平台兼容性: 该方法依赖于底层操作系统的具体实现,主要适用于 Windows 环境。在 Linux/macOS (.NET Core / .NET 5+) 上,行为可能有所不同或需要额外的库支持。
以上就是使用 C# TcpConnectionInformation 检测端口状态的详细内容,更多关于 C# 端口编程的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













