客户端: import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class ChatClient extends Frame { Socket s = null; DataOutputStream dos = null; DataInputStream dis = null; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); private boolean bConnected = false; public static void main(String[] args) { new ChatClient().launchFrame(); } public void launchFrame() { setLocation(300, 400); this.setSize(300, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent arg0) { disconnect(); System.exit(0); } }); tfTxt.addActionListener(new TFListener()); setVisible(true); connect(); new Thread(new RecvThread()).start(); } public void connect() { try { s = new Socket("172.18.56.6", 8888); dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); System.out.println("connect!");//确认连接成功 bConnected = true; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void disconnect(){ try { dos.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } private class TFListener implements ActionListener { public void actionPerformed(ActionEvent e) { String str = tfTxt.getText().trim(); //taContent.setText(str); tfTxt.setText(""); try { dos.writeUTF(str); dos.flush(); //dos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } private class RecvThread implements Runnable { public void run() { try { while(bConnected) { String str; str = dis.readUTF(); taContent.setText(taContent.getText() + str +'/n'); } } catch(SocketException e) { System.out.println("退出了,bye!"); } catch(EOFException e) { System.out.println("退出了,byebye!"); } catch(IOException e) { e.printStackTrace(); } } } } 服务器端: import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.List; public class ChatServer { boolean started = false;//测试服务器是否启动 ServerSocket ss = null; List<Client> clients = new ArrayList<Client>(); public static void main(String[] args) { new ChatServer().start(); } public void start() { try { ss = new ServerSocket(8888);//服务器启动 started = true;//测试启动成功 } catch(BindException e) { System.out.println("端口使用中"); System.out.println("请关闭相关程序并重新运行服务器"); System.exit(0); }catch(IOException e) { e.printStackTrace(); } try { while(started) { //启动后开始连接 boolean bConnected = false;//测试对方是否连接 Socket s = ss.accept(); //接受对方连接 Client c = new Client(s);//new新线程将连接引入 System.out.println("a client connected!"); new Thread(c).start();//启动线程 clients.add(c);//将线程存入clients集合 //dis.close(); } } catch(IOException e) { e.printStackTrace(); } finally { try { ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class Client implements Runnable { private Socket s; private DataInputStream dis = null; private DataOutputStream dos = null; private boolean bConnected = false; public Client(Socket s) { this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); bConnected = true;//确立和客户端连接 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void send(String str) throws IOException { try{ dos.writeUTF(str); } catch(IOException e) { clients.remove(this); System.out.println("对方退出了,我从List里面去掉了!"); } } public void run() { try { while(bConnected)//连接到后开始不断接受数据 { String str = dis.readUTF(); System.out.println(str); for(int i = 0; i < clients.size(); i++) { Client c = clients.get(i); c.send(str); } } } catch (IOException e) { //e.printStackTrace(); System.out.println("Client closed!"); } finally { try { if(dis != null) dis.close(); if(dos != null) dos.close(); if (s != null) s.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }