如何在C#console应用程序中执行telnet(How to do telnet in C# console application)

编程入门 行业动态 更新时间:2024-10-22 16:41:29
如何在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(); } }

更多推荐

本文发布于:2023-08-05 06:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   如何在   application   console   telnet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!