java應用Socket完成聊天室功用實例。本站提示廣大學習愛好者:(java應用Socket完成聊天室功用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是java應用Socket完成聊天室功用實例正文
最近研討了下Java socket通訊根底,應用代碼完成了一個復雜的多人聊天室功用,現把代碼共享下,希望能幫到有興味理解的人。
目錄構造:
ChatClient:
package com.panda.chat; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; @SuppressWarnings("serial") public class ChatClient extends Frame { private TextArea ta = new TextArea(); private TextField tf = new TextField(); private DataOutputStream dos = null; private DataInputStream dis = null; private Socket socket = null; private boolean bConnected = false; private Thread thread=null; public static void main(String[] args) { new ChatClient().frameClient(); } public void frameClient(){ setSize(400, 400); setLocation(400,300); add(ta,BorderLayout.NORTH); add(tf,BorderLayout.SOUTH); pack(); tf.addActionListener(new TfListener()); //封閉窗口事情監聽 this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { disconnected(); System.exit(0); } }); this.connect(); setVisible(true); } //鏈接服務器地址 private void connect(){ try { socket = new Socket("127.0.0.1", 8888); thread=new Thread(new ChatThread()); thread.start(); dos = new DataOutputStream(socket.getOutputStream()); dis = new DataInputStream(socket.getInputStream()); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //斷開銜接 private void disconnected(){ bConnected = false; try { dos.close(); dis.close(); socket.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } //鍵盤回車事情 private class TfListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String strMsg = tf.getText(); tf.setText(""); try { dos.writeUTF(strMsg); dos.flush(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } //開啟線程承受服務器信息 private class ChatThread implements Runnable{ @Override public void run() { try { bConnected = true; while(bConnected){ String msg = dis.readUTF(); String taText = ta.getText(); ta.setText(taText+msg+"\n"); } } catch (SocketException e) { System.out.println("加入");; } catch (IOException e) { e.printStackTrace(); } } } }
ChatServer:
package com.panda.chat; import java.io.*; import java.net.*; import java.util.*; public class ChatServer { private boolean started = false; private List<ChatThread> chatThreads = new ArrayList<ChatThread>(); public static void main(String[] args) { new ChatServer().startServer(); } private void startServer(){ try { //開啟服務端Socket ServerSocket seso = new ServerSocket(8888); started = true; while(started){ //承受客戶端銜接懇求 Socket sos = seso.accept(); System.out.println("一個客戶端已銜接"); //開啟線程處置客戶端通訊 ChatThread ct = new ChatThread(sos); chatThreads.add(ct); new Thread(ct).start(); } } catch (IOException e) { e.printStackTrace(); } } private class ChatThread implements Runnable{ private Socket socket; private DataInputStream din=null; private DataOutputStream don=null; private boolean bConnected = false; public ChatThread(Socket socket) { super(); this.socket = socket; } //發送信息的函數 private void send(String strMsgIn){ try{ don.writeUTF(strMsgIn); don.flush(); }catch(IOException e){ e.printStackTrace(); } } @Override public void run() { try{ din = new DataInputStream(socket.getInputStream()); don = new DataOutputStream(socket.getOutputStream()); //讀取數據 bConnected = true; while(bConnected){ String strMsgIn = din.readUTF(); System.out.println(strMsgIn); //接納到數據後發送給每個客戶端 for(int i =0;i<chatThreads.size();i++){ chatThreads.get(i).send(strMsgIn); } } }catch (IOException e) { try { //假如客戶端出錯或封閉,直接封閉銜接,並移除List中的以後線程 socket.close(); chatThreads.remove(this); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } finally{ try { din.close(); don.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
運轉ChatSever後,再同時翻開屢次ChatClient,就可以完成多人聊天了,你也試試。
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支持。