2009-06-27 31 views

回答

4

对于简单的任务(例如连接到具有类似Telnet的接口的专用硬件装置)经由套接字连接,只是发送和接收文本命令可能就足够了。

如果你想连接到真正的telnet服务器,你可能需要处理telnet转义序列,面对终端仿真,处理交互命令等。使用一些已经测试的代码,如Minimalistic Telnet library from CodeProject(免费)或一些商用的Telnet/Terminal Emulator库(例如我们的Rebex Telnet)可能会为您节省一些时间。

下面的代码(从this url拍摄)展示了如何使用它:

// 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(); 
相关问题