?? chatapplet.java
字號:
package chatapplet;import java.awt.*;import java.awt.event.*;import java.applet.*;import java.net.*;import java.io.*;import java.util.*;/** * Title: 客戶端Applet小程序 * Description: 利用Socket建立聊天室客戶端Applet小程序。 * Copyright: Copyright (c) 2002 * Company: 中國農(nóng)業(yè)大學計算機系 * @author 彭波 * @version 1.0 */ public class chatApplet extends Applet { Panel panel1 = new Panel(); // 放置輸入姓名和連接的兩個按鈕 BorderLayout borderLayout1 = new BorderLayout(); Panel panel2 = new Panel(); // 放置聊天信息顯示和聊天人員列表 Panel panel3 = new Panel(); // 放置發(fā)送信息區(qū)域 FlowLayout flowLayout1 = new FlowLayout(); FlowLayout flowLayout2 = new FlowLayout(); Label label1 = new Label(); TextField name_txt = new TextField(15); Button button1 = new Button(); Button button2 = new Button(); TextArea chat_txt = new TextArea(15,30); Label label2 = new Label(); Button button3 = new Button(); TextField msg_txt = new TextField(20); java.awt.List list1 = new java.awt.List(13); Socket soc=null; // 定義連接套接字 PrintStream ps=null; // 定義打印輸出流 Listen listen=null; // 定義一個客戶端線程監(jiān)聽 public void init(){ // 初始化圖形界面 resize(475,350); this.setLayout(borderLayout1); panel2.setLayout(flowLayout1); panel3.setLayout(flowLayout2); label1.setText("姓名:"); button1.setLabel("連接"); button2.setLabel("斷開連接"); chat_txt.setEditable(false); panel2.setBackground(Color.cyan); panel1.setBackground(Color.cyan); label2.setText("聊天信息:"); button3.setLabel("發(fā)送"); msg_txt.setText("請輸入聊天信息"); panel3.setBackground(Color.cyan); this.add(panel1, BorderLayout.NORTH); panel1.add(label1, null); panel1.add(name_txt, null); panel1.add(button1, null); panel1.add(button2, null); this.add(panel2, BorderLayout.CENTER); panel2.add(chat_txt, null); panel2.add(list1, null); this.add(panel3, BorderLayout.SOUTH); panel3.add(label2, null); panel3.add(msg_txt, null); panel3.add(button3, null); } public boolean action(Event evt,Object obj) { // 事件處理代碼 if(evt.target instanceof Button){ String label=(String) obj; if(label.equals("連接")){ // 點擊連接 if(soc==null){ try{ soc=new Socket(InetAddress.getLocalHost(),8080); // 使用端口8080實例化本地套接字 System.out.println(soc); // 打印實例化結果 ps=new PrintStream(soc.getOutputStream()); // 將ps指向soc的輸出流 StringBuffer info=new StringBuffer("INFO: "); // 定義字符緩沖存儲發(fā)送信息 // 其中INFO為關鍵字讓服務器識別連接信息 String userinfo=name_txt.getText()+":"+InetAddress.getLocalHost().toString(); // 將name和ip用":"分開 ps.println(info.append(userinfo)); ps.flush(); listen=new Listen(this,name_txt.getText(),soc); // 實例化客戶端線程 listen.start(); // 啟動線程 } catch(IOException e){ System.out.println("Error:"+e); disconnect(); } } // end of if } // end of if else if(label.equals("斷開連接")){ // 點擊斷開連接按鈕 disconnect(); } else if(label.equals("發(fā)送")){ // 點擊發(fā)送按鈕 if(soc!=null) { StringBuffer msg=new StringBuffer("MSG: "); // 定義并實例化字符緩沖存儲發(fā)送聊天信息 try { String msgtxt=new String(msg_txt.getText()); } catch(Exception e){} ps.println(msg.append(msg_txt.getText())); // 打印輸出流發(fā)送聊天信息 ps.flush(); } } } return true; } // end of method action public void disconnect(){ // 客戶端斷開連接方法 if(soc!=null){ try { listen.suspend(); ps.println("QUIT"); // 發(fā)送QUIT信息通知服務器斷開此次通訊 ps.flush(); soc.close(); // 關閉套接字 } catch(IOException e) { System.out.println("Error:"+e); } finally { } } // end of if } class Listen extends Thread{ // 定義客戶端線程類用于監(jiān)聽服務器傳來的信息 String name=null; // 存儲客戶端連接后的name信息 DataInputStream dis=null; // 實現(xiàn)客戶端接收服務器數(shù)據(jù)的輸入流 PrintStream ps=null; // 實現(xiàn)從客戶端發(fā)送數(shù)據(jù)到服務器的打印輸出流 Socket socket=null; // 存儲客戶端的socket信息 chatApplet parent=null; // 存儲當前運行的chatApplet實例 public Listen(chatApplet p,String n,Socket s) { // Listen類的構造器 parent=p; name=n; socket=s; try{ // 實例化兩個數(shù)據(jù)流 dis=new DataInputStream(s.getInputStream()); ps=new PrintStream(s.getOutputStream()); } catch(IOException e) { System.out.println("Error:"+e); parent.disconnect(); } } // end of Listen constractor public void run(){ // 定義線程run()方法 String msg=null; while(true) { try{msg=dis.readLine();} // 讀取從服務器傳來的信息 catch(IOException e) { System.out.println("Error:"+e); parent.disconnect(); } if (msg==null) { // 從服務器傳來的信息為空則斷開此次連接 parent.listen=null; parent.soc=null; parent.list1.clear(); return; } StringTokenizer st=new StringTokenizer(msg,":"); // 讀取分段字符 String keyword=st.nextToken(); // 讀取信息頭(關鍵字)識別何種信息 if(keyword.equals("PEOPLE")){ // PEOPLE為服務器發(fā)送的客戶連接信息 parent.list1.clear(); while(st.hasMoreTokens()){ // 遍歷st取得目前所連接的客戶 String str=st.nextToken(); parent.list1.addItem(str); } } else if(keyword.equals("MSG")){ // MSG為服務器傳送的聊天信息,用于顯示 String usr=st.nextToken(); parent.chat_txt.appendText(usr); parent.chat_txt.appendText(st.nextToken("\0")); parent.chat_txt.appendText("\n\n"); } else if(keyword.equals("QUIT")){ // QUIT為服務器關閉的信息,切斷連接 System.out.println("Quit"); try { parent.listen.stop(); parent.listen=null; parent.soc.close(); parent.soc=null; } catch(IOException e) { System.out.println("Error:"+e); } parent.list1.clear(); return; } } } // end of run method } // end of Listen inner class } // end of chatApplet class
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -