复习了一天Java。晚上写了一个HeartChat 0.1,实现多个客户端相互聊天的机制。代码如下:
 

Java代码 Java实现多个客户端聊天程序 _客户端 Java实现多个客户端聊天程序 _Java_02
  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import java.io.*;   
  4. import java.lang.*;   
  5. import java.net.*;   
  6.   
  7. public class HeartClient extends Frame {   
  8.   
  9.     /*  
  10.      *  成员方法出场...  
  11.      */  
  12.     private TextField tfText;   
  13.     private TextArea taContent;   
  14.     private Socket s;   
  15.     private DataOutputStream dos;   
  16.     private DataInputStream dis;   
  17.        
  18.     /**  
  19.      * 注意,入口... ^^  
  20.      * @param args  
  21.      */  
  22.     public static void main(String[] args) {   
  23.         new HeartClient().launchFrame();   
  24.            
  25.     }   
  26.        
  27.     /**  
  28.      * Loading GU  
  29.      */  
  30.     public void launchFrame(){   
  31.         tfText = new TextField();   
  32.         taContent = new TextArea();   
  33.         this.setSize(300,300);   
  34.         this.setLocation(300,300);   
  35.         this.tfText.addActionListener(new TFListener());   
  36.         this.add(tfText,BorderLayout.SOUTH);   
  37.         this.add(taContent,BorderLayout.NORTH);   
  38.         this.addWindowListener(new WindowAdapter(){   
  39.             @Override  
  40.             public void windowClosing(WindowEvent e) {   
  41.                 System.exit(0);   
  42.             }});   
  43.         this.pack();   
  44.         this.connect();   
  45.         this.setVisible(true);   
  46.     }   
  47.   
  48.     /**  
  49.      * 我在努力地连接服务器中...  
  50.      */  
  51.     public void connect() {   
  52.         try {   
  53.             s = new Socket("127.0.0.1",1720);   
  54.             dos = new DataOutputStream(s.getOutputStream());   
  55.             dis = new DataInputStream(s.getInputStream());   
  56.             new Thread(new SendThread()).start();   
  57. //          dos.writeUTF("Hello,i find u!");   
  58.         } catch (UnknownHostException e) {   
  59.             System.out.println("UnknownHostException");   
  60.             e.printStackTrace();   
  61.         } catch (IOException e) {   
  62.             System.out.println("IOException");   
  63.             e.printStackTrace();   
  64.         }finally{   
  65.             //关闭啥尼...   
  66.         }   
  67.            
  68.     }   
  69.   
  70.     /**  
  71.      * 额不会傻等着tfText(TextField tfText的监听器类)  
  72.      */  
  73.     class TFListener implements ActionListener{   
  74.         private String str;   
  75.         @Override  
  76.         public void actionPerformed(ActionEvent e) {   
  77.             str = tfText.getText().trim();   
  78.             tfText.setText("");   
  79.             try {   
  80.                 dos.writeUTF(str);   
  81.             } catch (IOException e1) {   
  82.                 System.out.println("IOException");   
  83.                 e1.printStackTrace();   
  84.             }   
  85.         }   
  86.            
  87.     }   
  88.        
  89.     /**  
  90.      * 客户端接收消息的线程呦...  
  91.      *  
  92.      */  
  93.     class SendThread implements Runnable{   
  94.         private String str;   
  95.         private boolean iConnect = false;   
  96.            
  97.         public void run(){   
  98.             iConnect = true;   
  99.             recMsg();   
  100.                
  101.         }   
  102.            
  103.         /**  
  104.          * 消息,看招,哪里跑...(客户端接收消息的实现)  
  105.          * @throws IOException   
  106.          */  
  107.         public void recMsg() {   
  108.             try {   
  109.                 while(iConnect){   
  110.                     str = dis.readUTF();   
  111.                     taContent.setText(str);   
  112.                 }   
  113.             } catch (IOException e) {   
  114.                 e.printStackTrace();   
  115.             }   
  116.                
  117.         }   
  118.            
  119.     }   
  120.        
  121. }