这是服务端
import java.io.*;
import java.net.*;public class ChatServer {
ServerSocket ss = null;
boolean start = false; public static void main(String[] args) {
ChatServer server = new ChatServer();
server.start();
} public void start() {
try {
ss = new ServerSocket(6666);
start = true;
while (start) { Socket s = null;
s = ss.accept();
System.out.println("一个客户端链接成功");
ClientThread client = new ClientThread(s);
Thread tt = new Thread(client);
tt.start(); }
} catch (IOException e) {
System.out.println("申请绑定端口失败");
}
} class ClientThread implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private boolean sts = false; public ClientThread(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
sts = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} @Override
public void run() {

while (sts) {
try {
System.out.println("zhi xing dao sssssssssle ma ");
System.out.println(dis.readUTF());
System.out.println("zhi xing dao le ma ");
} catch (EOFException w) {
System.out.println("client close");

} catch (IOException e) {
System.out.println("a client slose");
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
} } }

}
}
}
这是客户端
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.*;
import java.net.*;public class ChatClient { public static void main(String[] args) { MyFrame qq = new MyFrame();
qq.lauchFrame();
}}class MyFrame extends Frame {
Socket s;
OutputStream os;
DataOutputStream dos;
TextField read = new TextField();
TextArea look = new TextArea(); public void lauchFrame() {
MyMonitor2 enter = new MyMonitor2();
read.addActionListener(enter);
this.setBounds(60, 60, 300, 500);
this.setBackground(Color.BLUE);
this.setTitle("111宿舍在线聊天系统 作者:袁琦");
this.setVisible(true);
this.add(read, BorderLayout.SOUTH);
this.add(look, BorderLayout.NORTH);
this.pack();
MyMonitor xx = new MyMonitor();
this.addWindowListener(xx);
this.connect();
} private class MyMonitor extends WindowAdapter {
public void windowClosing(WindowEvent e) {
setVisible(false);
deadConnect();
System.exit(-1);
} } public void connect() {
try {
try {
s = new Socket("127.0.0.1", 6666);
} catch (UnknownHostException w) {
System.out.println("链接服务器(主机)失败 ");
w.printStackTrace();
}
os = s.getOutputStream();
dos = new DataOutputStream(os);
} catch (UnknownHostException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} } public void deadConnect() {
try {
dos.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } private class MyMonitor2 implements ActionListener { public void actionPerformed(ActionEvent e) {
try {
dos.writeUTF(read.getText());
look.append("\n" + read.getText());
read.setText("");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
dos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

} }
}
客户端没错误,