这个是否直接明了?有没有什么好的例子?所有我在谷歌上搜索的结果都是如何在dotNet中制作telnet客户端,但这对于我来说过于复杂了。我正在尝试在C#中完成这个。
感谢!
C# 2.0和 Telnet - 没有想象的那么困难
http://geekswithblogs.net/bigpapa/archive/2007/10/08/C-2.0-and-Telnet---Not-As-Painful-As-It.aspx
或者使用这个备用链接.
如果你要使用 System.Net.Sockets 类,以下是你需要做的:
您还可以使用 System.Net.Sockets.TcpClient 对象代替 socket 对象,因为它已经配置了使用 ProtocolType.Tcp 的套接字参数。让我们看看该选项:
对于简单的任务(例如连接到具有类似telnet接口的专用硬件设备),通过套接字连接并仅发送和接收文本命令可能足够。
如果您想连接到真正的telnet服务器,则可能需要处理telnet转义序列,面对终端仿真,处理交互式命令等。使用一些已经经过测试的代码,例如CodeProject的极简Telnet库(免费)或某些商业Telnet /终端模拟器库(例如我们的Rebex Telnet),可能会节省您一些时间。
以下代码(取自此网址)显示了如何使用它:
// create the client
Telnet client = new Telnet("servername");
// start the Shell to send commands and read responses
Shell shell = client.StartShell();
// set the prompt of the remote server's shell first
shell.Prompt = "servername# ";
// read a welcome message
string welcome = shell.ReadAll();
// display welcome message
Console.WriteLine(welcome);
// send the 'df' command
shell.SendCommand("df");
// read all response, effectively waiting for the command to end
string response = shell.ReadAll();
// display the output
Console.WriteLine("Disk usage info:");
Console.WriteLine(response);
// close the shell
shell.Close();