模拟的unix主机实现telnet功能出现的问题:
模拟的unix主机实现telnet功能出现的问题:
下面是我模拟的unix主机实现telnet的代码,客户端采用的windows控制台,这段代码能够实现从控制台进行的telnet登录。例如,从控制台输入:telnet 192.168.1.2 7777 能够和我的服务器建立连接,并能接收服务器的返回信息。但是,出现一个问题,当连接建立以后,从控制台输入的字符在控制台看不到(例如:我输入:query,屏幕上不显示query),但是输入的信息可以被后台接受。后台返回的信息控制台也可以显示。谁能帮我解决一下,非常着急。先谢了。
public class VMS extends Thread
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private String file;
InetAddress address;
/**
*
* 构造函数
*
* @param s
* @param path
* @throws IOException
*/
public VMS(Socket s, String path) throws IOException
{
file = path;
socket = s;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
address = socket.getInetAddress();
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),
true);
}
public void run()
{
Document doc = new XmlTool(file).createDocument();
Map map = new XmlTool(file).getchildrenMap(doc, "common");
System.out.println("map:" + map);
String outStr = "";
out.println("请输入指令:(提示 help)");
String command = null;
boolean done = false;
try
{
while (!done)
{
String str = in.readLine();
System.out.println(str);
command = str.trim().toUpperCase();
if (str == null || command.equals("QUIT"))
// 命令quit结束本次连接
done = true;
else if (command.equals("HELP"))
{
// 命令help查询本服务器可接受的命令
out.println("query");
out.println("quit");
out.println("help");
}
else if (command.startsWith("QUERY"))
{
// 命令 query
out.println("query success!");
}
else if (!command.startsWith("HELP")
&& !command.startsWith("QUIT")
&& !command.startsWith("QUERY"))
{
out.println("Command not Found! Please refer to the HELP!");
}
}
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
//类Services
public class Services
{
/**
* 主应用程序
* @param args
*/
static final int PORT = 7777;
public static void main(String[] args) throws IOException
{
ServerSocket s = new ServerSocket(PORT);
System.out.println("Server started...");
try
{
while (true)
{
Socket socket = s.accept();
try
{
new VMS(socket,path).start();
}
catch (IOException e)
{
socket.close();
}
}
}
finally
{
s.close();
}
}
}