通过使用Socket尝试连接IP对应的端口来判断,代码如下:

//将IP和端口替换成为你要检测的
string ipAddress = "192.168.1.1";
int portNum = 22;
IPAddress ip = IPAddress.Parse(ipAddress);
try
{
	IPEndPoint point = new IPEndPoint(ip, portNum);
	using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
	{
		sock.Connect(point);
		Console.WriteLine("连接{0}成功!", point);
		sock.Close();
	} 
}
catch (SocketException e)
{
	Console.WriteLine("连接{0}失败", point);
}
已赞 5 0 评论