各位大哥!我问个简单的问题!请不要不屑回答!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Client {
DataOutputStream dos;
Socket client = null;
public TextField typeIn;
public TextArea output;
DataInputStream dis = null;
boolean connected = false;
public static void main(String[] args) {
Client c = new Client();
c.run();
}
public void run() {
frameClient clientDialogbox = new frameClient();
connect();
new Thread(new getserverInformation()).start();
}
public void connect() {
try {
client = new Socket("169.254.64.49", 8888);
dos = new DataOutputStream(client.getOutputStream());
dis = new DataInputStream(client.getInputStream());
connected = true;
} catch (UnknownHostException e) {
System.out.println("Wrong host!");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There's no server!");
//e.printStackTrace();
System.exit(0);
}
}
public void disconnect() {
try {
dos.close();
dis.close();
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sentToserver(String s) {
try {
dos.writeUTF(s);
dos.flush();
//System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}
}
private class frameClient extends Frame {
frameClient() {
this.setLocation(300, 300);
this.setSize(300, 200);
this.setBackground(Color.green);
this.setLayout(new BorderLayout(2, 1));
typeIn = new TextField();
typeIn.setBackground(Color.orange);
output = new TextArea();
output.setBackground(Color.green);
this.add(typeIn, BorderLayout.SOUTH);
this.add(output, BorderLayout.NORTH);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
disconnect();
System.out.println("Bye!");
System.exit(0);
}
});
typeIn.addActionListener(new listener());
this.setVisible(true);
}
private class listener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String in = typeIn.getText();
//output.setText(in);
typeIn.setText("");
sentToserver(in);
}
}
}
public class getserverInformation implements Runnable {
public void run() {
String readIn;
try {
while(connected) {
readIn = dis.readUTF();
String ins = typeIn.getText();
output.setText(ins + "\n" +readIn);
//System.out.println(readIn);
}
} catch (IOException e) {
try {
connected = false;
dis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Nothing to read!");
}
}
}
}
这里面有一个public class getserverInformation implements Runnable方法
在这一段里
while(connected) {
readIn = dis.readUTF();
String ins = typeIn.getText();//这个为什么是空的但是却并没有出错!
output.setText(ins + "\n" +readIn);
//System.out.println(readIn);
}