1. import java.io.IOException;  
  2. import java.net.*;  
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.io.*;  
  6.  
  7. public class ChatServer {  
  8.     boolean started = false;  
  9.     ServerSocket ss = null;  
  10.     List<Client> clients = new ArrayList<Client>();  
  11.       
  12.     public static void main(String[] args) {  
  13.         new ChatServer().start();  
  14.     }  
  15.  
  16.     public void start() {  
  17.         try {  
  18.             ss = new ServerSocket(8888);  
  19.             started = true;  
  20.         } catch (IOException ex) {  
  21.             System.out.println("Socket连接失败!端口使用中");  
  22.             ex.printStackTrace();  
  23.         }  
  24.  
  25.         try {  
  26.             while (started) {  
  27.                 Socket socket = ss.accept();  
  28.                 Client c = new Client(socket);//将对象引用传递给Client类  
  29.                 clients.add(c);  
  30.                 System.out.println("a client connected!");  
  31.                 new Thread(c).start();  //启用独立的线程接收客户端的消息  
  32.             }  
  33.         } catch (IOException e) {  
  34.             System.out.println("网络连接失败!");  
  35.         } finally {  
  36.             try {  
  37.                 ss.close();  
  38.             } catch (IOException e) {  
  39.                 e.printStackTrace();  
  40.             }  
  41.         }  
  42.     }  
  43.  
  44.     class Client implements Runnable {  
  45.         private Socket s = null;  
  46.         private DataInputStream dis = null;  
  47.         private DataOutputStream dos = null;  
  48.         private boolean bConnected = false;  
  49.  
  50.         Client(Socket s) {    
  51.             this.s = s;  
  52.             try {  
  53.                 dis = new DataInputStream(s.getInputStream());  
  54.                 dos = new DataOutputStream(s.getOutputStream());  
  55.                 bConnected = true;  
  56.                   
  57.             } catch (IOException e) {  
  58.                 e.printStackTrace();  
  59.             }  
  60.         }  
  61.  
  62.         public void send(String str){  
  63.             try {  
  64.                 dos.writeUTF(str);//发送消息  
  65.             } catch (IOException e) {  
  66.                 clients.remove(this);  
  67.                 //e.printStackTrace();  
  68.             }  
  69.         }  
  70.           
  71.         public void run() {  
  72.             try {  
  73.                 while (bConnected) {  
  74.                     String str = dis.readUTF();//接收消息  
  75.                     System.out.println(str);  
  76.                     for(int i = 0;i < clients.size(); i++) {  
  77.                         Client c = clients.get(i);  
  78.                         c.send(str);  
  79.                     }  
  80.                 }  
  81.             } catch (EOFException e) {  
  82.                 System.out.println("Client closed!");  
  83.             } catch (IOException e) {  
  84.                 e.printStackTrace();  
  85.             } finally {  
  86.                 try {  
  87.                     if (dis != null)    dis.close();  
  88.                     if(dos != null) dos.close();  
  89.                     if (s != null)  s.close();  
  90.                 } catch (IOException e) {  
  91.                     e.printStackTrace();  
  92.                 }  
  93.             }  
  94.         }  
  95.     }  
  96. }  

客户端

 

 
  
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. import java.io.*;  
  4. import java.net.Socket;  
  5. import java.net.SocketException;  
  6. import java.net.UnknownHostException;  
  7.  
  8. public class ChatClient extends Frame {  
  9.     private DataOutputStream dos = null;  
  10.     private DataInputStream dis = null;  
  11.     private boolean bConnected = false;  
  12.     Socket s = null;  
  13.       
  14.     private TextField tfText = new TextField();  
  15.     private TextArea caContent = new TextArea();  
  16.     Thread tRecv = new Thread(new Receive());//接收消息的线程  
  17.  
  18.     public static void main(String[] args) {  
  19.         new ChatClient().frameLaunch();  
  20.     }  
  21.  
  22.     private void frameLaunch() {  
  23.           
  24.         this.setLocation(500500);  
  25.  
  26.         tfText.addActionListener(new TFListener());  
  27.         add(tfText, BorderLayout.SOUTH);  
  28.         add(caContent, BorderLayout.NORTH);  
  29.         // this.setSize(400,200);   --与pack()同时使用时无效  
  30.         pack();  
  31.  
  32.         this.addWindowListener(new WindowAdapter() {//关闭窗口  
  33.             public void windowClosing(WindowEvent arg0) {  
  34.                 disConnected();//关闭窗口前先关闭连接  
  35.                 System.exit(0);  
  36.             }  
  37.         });  
  38.           
  39.         setVisible(true);  
  40.         connected();  
  41.         tRecv.start();  
  42.     }  
  43.  
  44.     public void connected() {  
  45.         try {  
  46.             s = new Socket("127.0.0.1"8888);  
  47.             dos = new DataOutputStream(s.getOutputStream());  
  48.             dis = new DataInputStream(s.getInputStream());  
  49.             bConnected = true;//标记已连接上  
  50.             System.out.println("connected successfully!");  
  51.  
  52.         } catch (UnknownHostException e) {  
  53.             System.out.println("服务器未启动!");  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.  
  60.     public void disConnected() {  
  61.         /*    
  62.         try {  
  63.             bConnected = false;  
  64.             tRecv.join();           //在Receive中仍然可能陷入死循环  
  65.         } catch (InterruptedException e) {  
  66.             e.printStackTrace();  
  67.         } finally {  
  68.             try {  
  69.                 dos.close();  
  70.                 dis.close();  
  71.                 s.close();  
  72.             } catch (IOException e) {  
  73.                 e.printStackTrace();  
  74.             }  
  75.         }  
  76.         */ 
  77.         try {  
  78.             dos.close();  
  79.             dis.close();  
  80.             s.close();  
  81.         } catch (IOException e) {  
  82.             e.printStackTrace();  
  83.         }  
  84.           
  85.     }  
  86.  
  87.     private class TFListener implements ActionListener {//内部类,不需要外部调用  
  88.           
  89.         public void actionPerformed(ActionEvent e) {  
  90.             String str = tfText.getText().trim();  
  91.             tfText.setText("");  
  92.             sendMessage(str);  
  93.         }  
  94.     }  
  95.  
  96.     private void sendMessage(String str) {  
  97.         try {  
  98.             dos.writeUTF(str);  
  99.             dos.flush();  
  100.         } catch (IOException e) {  
  101.             e.printStackTrace();  
  102.         }  
  103.     }  
  104.       
  105.     class Receive implements Runnable {  
  106.         public void run() {  
  107.             try {  
  108.                 while(bConnected){  
  109.                     String str = dis.readUTF();  
  110.                     System.out.println(str);  
  111.                     caContent.setText(caContent.getText() + str + "\n");  
  112.                 }  
  113.                   
  114.             } catch (SocketException e) {  
  115.                 System.out.println("退出了,bye!");  
  116.             } catch (IOException e) {  
  117.                 e.printStackTrace();  
  118.             }   
  119.         }  
  120.     }  
  121. }  
  122.