1

在 C# 控制台应用程序中需要执行 telnet 实用程序并知道所需端口是否打开

   var ping = new Ping();
        var rply = ping.Send("192.168.1.117");
        if (rply.Status == IPStatus.Success)
        {
            Console.WriteLine("up");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);

          }
        else
        {
            Console.WriteLine("down");
            Console.WriteLine("Press any key to continue");
            Console.ReadKey(true);

我正在使用上面的代码来 ping,但是对于 telnet 和端口我应该怎么做,以便在控制台应用程序中它应该执行 telnet 实用程序并让用户知道所需的端口是打开的

4

1 回答 1

6

一般来说,服务器管理员不喜欢你连接然后丢弃(查看端口是否打开的唯一真正方法)。但是,如果你想这样做,你可以这样做:

TcpClient tc = null;
try
{
    tc = new TcpClient("192.168.1.117", 23);
    // If we get here, port is open
} 
catch(SocketException se) 
{
    // If we get here, port is not open, or host is not reachable
}
finally
{
   if (tc != null)
   {
      tc.Close(); 
   }
}
于 2012-10-08T21:40:39.397 回答