簡單的聊天程式(java的socket+多執行緒)
阿新 • • 發佈:2018-12-13
服務端 import java.net.*; import java.util.*; import java.io.*; public class ChatServer { ServerSocket server = null; Collection cClient = new ArrayList();//裝執行緒的容器 public ChatServer(int port) throws Exception { server = new ServerSocket(port); } public void startServer() throws Exception { while (true) { Socket s = server.accept(); cClient.add(new ClientConn(s)); System.out.println("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort()); System.out.println("CLIENTS-COUNT: " + cClient.size() + "\n"); } } //多執行緒 class ClientConn implements Runnable { Socket s = null; public ClientConn(Socket s) { this.s = s; (new Thread(this)).start(); } public void send(String str) throws IOException { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(str); } public void dispose() { try { if (s != null) s.close(); cClient.remove(this); System.out.println("A client out!"); System.out.println("CLIENT-COUNT: " + cClient.size() + "\n"); } catch (Exception e) { e.printStackTrace(); } } public void run() { try { DataInputStream dis = new DataInputStream(s.getInputStream()); String str = dis.readUTF(); while (str != null && str.length() != 0) { System.out.println(s.getInetAddress() + "-" + s.getPort() + ": " + str); for (Iterator it = cClient.iterator(); it.hasNext();) { ClientConn cc = (ClientConn) it.next(); //if (this != cc) { cc.send(s.getInetAddress() + "-" + s.getPort() + ": " + str); //} } str = dis.readUTF(); //send(str); } this.dispose(); } catch (Exception e) { System.out.println("client quit"); this.dispose(); } } } public static void main(String[] args) throws Exception { ChatServer cs = new ChatServer(8888); cs.startServer(); } }
客戶端 import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class ChatClient extends Frame { TextArea ta = new TextArea(); TextField tf = new TextField(); Socket s = null; public void launchFrame() throws Exception { this.add(ta, BorderLayout.CENTER); this.add(tf, BorderLayout.SOUTH); tf.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { String sSend = tf.getText(); if (sSend.trim().length() == 0) { System.out.println("輸入為空!"); return; } ChatClient.this.send(sSend); tf.setText(""); ta.append("me: " + sSend + "\n"); } catch (Exception e) { e.printStackTrace(); } } } ); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); setBounds(300,300,300,400); setVisible(true); ta.requestFocus(); } public ChatClient() throws Exception { s = new Socket("39.108.12.42", 9050); launchFrame(); (new Thread(new ReceiveThread())).start(); } //向伺服器傳送資料 public void send(String str) throws Exception { DataOutputStream dos = new DataOutputStream(s.getOutputStream()); dos.writeUTF(str); } public void disconnect() throws Exception { s.close(); } class ReceiveThread implements Runnable { public void run() { if (s == null) return; try { DataInputStream dis = new DataInputStream(s.getInputStream()); String str = dis.readUTF(); while (str != null && str.length() != 0) { //System.out.println(str); ChatClient.this.ta.append(str + "\n"); str = dis.readUTF(); } } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) throws Exception { ChatClient cc = new ChatClient(); } }