ChatClient.java
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import java.net.*; import java.io.*; public class ChatClient extends Frame { /** * 版本end */ Socket s = null ; DataOutputStream dos = null; DataInputStream dis = null; private boolean bConnected = false; TextField tfTxt = new TextField(); //敲回车时有反应 ActionEvent事件 三种方法:平列的类,匿名类,内部类 TextArea taContent = new TextArea(); Thread tRecv = new Thread(new RecvThread()); public void launchFrame() { setLocation(400, 300); // 设置位置 this.setSize(300, 300); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { // 匿名类 @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub disconnect(); System.exit(0); // 传0正常退出 } }); tfTxt.addActionListener(new TFListener()); this.setVisible(true); connect(); tRecv.start(); } private class TFListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String str = tfTxt.getText().trim(); //String 中trim()方法去掉两边空格 //taContent.setText(str); tfTxt.setText(""); //发送到服务器中 try { dos.writeUTF(str); dos.flush(); //dos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } //连接服务器的方法 public void connect(){ try{ s = new Socket("127.0.0.1", 8888); //对外的输出流 dos = new DataOutputStream(s.getOutputStream()); dis = new DataInputStream(s.getInputStream()); System.out.println("connected!"); bConnected = true; }catch(UnknownHostException e){ //找不到主机 e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } public void disconnect(){ // try { // bConnected = false; // tRecv.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // }finally{ // try { // dos.close(); // dis.close(); // s.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } try { dos.close(); dis.close(); s.close(); } catch (IOException e) { e.printStackTrace(); } } private class RecvThread implements Runnable{ public void run() { try{ while(bConnected){ String str = dis.readUTF(); //System.out.println(str); taContent.setText(taContent.getText() + str + '\n'); } }catch(SocketException e){ System.out.println("退出了,bye!"); }catch(EOFException e){ System.out.println("退出了,bye!"); }catch(IOException e){ e.printStackTrace(); } } } public static void main(String[] args) { // TODO Auto-generated method stub new ChatClient().launchFrame(); } }
ChatServer.java
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.net.*; import java.util.*; 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(); System.exit(0);//结束应用程序 } try{ //接受客户端的连接 while(started){ Socket s = ss.accept(); Client c = new Client(s); System.out.println("a client connected!"); //调试性的语句放在最右边 new Thread(c).start(); clients.add(c); } }catch (IOException e) { e.printStackTrace(); }finally{ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } class Client implements Runnable{ private Socket s; private DataInputStream dis = null; private boolean bConnected = false; private DataOutputStream dos = null; public Client(Socket s){ this.s = s; try { dis = new DataInputStream(s.getInputStream()); dos = new DataOutputStream(s.getOutputStream()); bConnected = true; } catch (IOException e) { e.printStackTrace(); } } public void send(String str){ try { dos.writeUTF(str); } catch (IOException e) { clients.remove(this); System.out.println("对方退出了,我从List中去除了"); //e.printStackTrace(); } } 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); } // for(Iterator<Client> it = clients.iterator(); it.hasNext();){ //内部锁定,效率不高 // Client c = it.next(); // c.send(str); // } // Iterator<Client> it = clients.iterator(); // while(it.hasNext()){ // Client c = it.next(); // c.send(str); // } } } catch(EOFException e){ System.out.println("Client closed !"); }catch (IOException e) { e.printStackTrace(); }finally{ try{ if(dis != null) dis.close(); if(dos != null) dos.close(); if(s != null) { s.close(); s = null; } }catch(IOException el){ el.printStackTrace(); } } } } }