做一个telnet客户端,模拟windows系统自带的telnet功能。
问题1:用tcp连接接收到的文件部分(特殊字符之类)是乱码。
问题2:我接到了包括乱码在内的很多数据,放到一个String打印的时候能看见,通过TextArea.setText(String)这样显示的只是部分乱码,很少的。
下面是代码:麻烦各位帮我看看,代码复制过去能直接运行的,搞定了给全分。 package telnet;import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;@SuppressWarnings("serial")
public class Client extends Frame{
BorderLayout bl= new BorderLayout();
GridLayout  gl = new GridLayout();
Panel north = new Panel();           //上面的输入框和按钮
Panel south = new Panel();                //下面的输入框和按钮
Button btnNorth = new Button("输入您要连接的URL地址:");
Button btnSouth = new Button("发送信息:");
TextField tfNorth =new TextField();  //上面输入框
TextField tfSouth =new TextField(); //下面输入框
TextArea  ta = new TextArea();    //中间显示框

public Client(String title){
super(title);
setLayout(bl);
setBounds(50, 50, 600,400);
north.setLayout(gl);
south.setLayout(gl);
ta.setEditable(false);
ta.setBackground(Color.black);
ta.setForeground(Color.white);
north.add(btnNorth);
north.add(tfNorth);
south.add(btnSouth);
south.add(tfSouth);
add(north, BorderLayout.NORTH);
add(south, BorderLayout.SOUTH);
add(ta, BorderLayout.CENTER);
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt){
System.exit(0);
}
}); //连接并接收返回数据
btnNorth.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("正在连接..........");
String result=getInfo();
                                System.out.println(result);       //能打印值
ta.setText(result);      //出问题的地方。不能设置正确值
setVisible(true);
System.out.println("连接成功..........");
}
});
//发送新数据并接收返回数据
btnSouth.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
getMore();
}
});

} //连接到bbs
public String getInfo() {
// String url = tfNorth.getText();
StringBuffer info = new StringBuffer("\r\n");            //临时 
String result = null;      //接收并返回得到的数据
try {
// Socket s = new Socket(url, 23);
Socket s = new Socket("bbs.nuc.edu.cn", 23);
s.setSoTimeout(3000);               //连接3秒没数据到达就处理数据
BufferedReader in = new BufferedReader(new InputStreamReader(s
.getInputStream()));  //出现部分乱码
                        //DataInputStream in = new DataInputStream(s.getInputStream());//也乱码
boolean flag = true;
if (flag) {
String line = in.readLine();
if (line != null) {
info.append(line + "\r\n");
} else {
flag = false;
}
}
 result = info.toString();
return result;
}catch(SocketTimeoutException e){
result = info.toString();
return result;
} catch (UnknownHostException e) {
e.printStackTrace();
return new String("找不到主机!!!");
} catch (IOException e) {
e.printStackTrace();
return new String("网络异常!!!");
}
} protected void getMore() {
//未实现
}

public static void main(String[] args) {
new Client("telnet");
}

}