C# 使用TcpClient 测试网络连接是否正常
using System.Net.Sockets; TcpClient client = new TcpClient(); //参数1指定ip地址,参数2指定端口 var result = client.BeginConnect("127.0.0.1", 80, null, null); //TimeSpan.FromSeconds(1) 表示测试连接1秒,即超时时间 var success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1)); client.Close(); Console.WriteLine(success);