在 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 实用程序并让用户知道所需的端口已打开
最佳答案
一般来说,服务器管理员不喜欢你连接然后断开(查看端口是否打开的唯一真实方法)。但是,如果你想这样做,你可以这样做:
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();
}
}
关于c# - 如何在 C# 控制台应用程序中执行 telnet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789830/