如何在C#console应用程序中执行telnet(How to do telnet in C# console application)
在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和port我应该怎么做,以便在控制台应用程序中它应该执行telnet实用程序并让用户知道所需的端口是打开的
In C# console application need to perform telnet utility and know whether the required port is open
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);I am using above code to ping but for telnet and port how should i do it, so that in console application it should perform telnet utility and let the user know the required port is open
最满意答案
一般来说,服务器管理员不喜欢你连接然后丢弃(唯一真正的方法来查看端口是否打开)。 但是,如果你想这样做,你可以这样做:
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(); } }Generally speaking, server admins don't like you connecting and then dropping (the only real way to see if a port is open). However, if you want to do it you can do:
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(); } }更多推荐
发布评论