java聊天室的實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了java實(shí)現(xiàn)聊天室的具體代碼,供大家參考,具體內(nèi)容如下
聊天室界面:
源碼:
public class ClientFrame extends Frame { private TextField textFieldContent = new TextField(); private TextArea textAreaContent = new TextArea(); private Socket socket = null; private OutputStream out = null; private DataOutputStream dos = null; private InputStream in = null; private DataInputStream dis = null; private boolean flag = false; /** * 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:19:51 功能:?jiǎn)?dòng)客戶端程序 * * @param args */ public static void main(String[] args) { new ClientFrame().init(); } /** * 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:20:43 功能:對(duì)窗口進(jìn)行初始化 */ private void init() { this.setSize(300, 300); setLocation(250, 150); setVisible(true); setTitle("WeChatRoom"); // 添加控件 this.add(textAreaContent); this.add(textFieldContent, BorderLayout.SOUTH); textAreaContent.setFocusable(false); pack(); // 關(guān)閉事件 addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("用戶試圖關(guān)閉窗口"); disconnect(); System.exit(0); } }); // textFieldContent添加回車事件 textFieldContent.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { onClickEnter(); } }); // 建立連接 connect(); new Thread(new ReciveMessage()).start(); } private class ReciveMessage implements Runnable { @Override public void run() { flag = true; try { while (flag) { String message = dis.readUTF(); textAreaContent.append(message + "\n"); } } catch (EOFException e) { flag = false; System.out.println("客戶端已關(guān)閉"); // e.printStackTrace(); } catch (SocketException e) { flag = false; System.out.println("客戶端已關(guān)閉"); // e.printStackTrace(); } catch (IOException e) { flag = false; System.out.println("接受消息失敗"); e.printStackTrace(); } } } /** * 功能:當(dāng)點(diǎn)擊回車時(shí)出發(fā)的事件 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午9:49:30 */ private void onClickEnter() { String message = textFieldContent.getText().trim(); if (message != null && !message.equals("")) { String time = new SimpleDateFormat("h:m:s").format(new Date()); textAreaContent.append(time + "\n" + message + "\n"); textFieldContent.setText(""); sendMessageToServer(message); } } /** * 功能:給服務(wù)器發(fā)送消息 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:13:48 * * @param message */ private void sendMessageToServer(String message) { try { dos.writeUTF(message); dos.flush(); } catch (IOException e) { System.out.println("發(fā)送消息失敗"); e.printStackTrace(); } } /** * 功能:申請(qǐng)socket鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:00:38 */ private void connect() { try { socket = new Socket("localhost", 8888); out = socket.getOutputStream(); dos = new DataOutputStream(out); in = socket.getInputStream(); dis = new DataInputStream(in); } catch (UnknownHostException e) { System.out.println("申請(qǐng)鏈接失敗"); e.printStackTrace(); } catch (IOException e) { System.out.println("申請(qǐng)鏈接失敗"); e.printStackTrace(); } } /** * 功能:關(guān)閉流和鏈接 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:01:32 */ private void disconnect() { flag = false; if (dos != null) { try { dos.close(); } catch (IOException e) { System.out.println("dos關(guān)閉失敗"); e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { System.out.println("dos關(guān)閉失敗"); e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { System.out.println("socket關(guān)閉失敗"); e.printStackTrace(); } ; } } }
package com.chat; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.BindException; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; public class ChatServer { private List<Client> clients = new ArrayList<>(); /** * 功能:?jiǎn)?dòng)ChatSever 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:26:41 * * @param args */ public static void main(String[] args) { new ChatServer().init(); } /** * 功能:對(duì)chatserver初始化 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午10:27:09 */ private void init() { System.out.println("服務(wù)器已開啟"); // BindException ServerSocket ss = null; Socket socket = null; try { ss = new ServerSocket(8888); } catch (BindException e) { System.out.println("端口已被占用"); e.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { Client client = null; while (true) { socket = ss.accept(); System.out.println("客戶駕到"); client = new Client(socket); clients.add(client); new Thread(client).start(); } } catch (IOException e) { e.printStackTrace(); } } private class Client implements Runnable { private Socket socket = null; InputStream in = null; DataInputStream din = null; OutputStream out = null; DataOutputStream dos = null; boolean flag = true; public Client(Socket socket) { this.socket = socket; try { in = socket.getInputStream(); din = new DataInputStream(in); } catch (IOException e) { System.out.println("接受消息失敗"); e.printStackTrace(); } } public void run() { String message; try { while (flag) { message = din.readUTF(); // System.out.println("客戶說:" + message); forwordToAllClients(message); } } catch (SocketException e) { flag = false; System.out.println("客戶下線"); clients.remove(this); // e.printStackTrace(); } catch (EOFException e) { flag = false; System.out.println("客戶下線"); clients.remove(this); // e.printStackTrace(); } catch (IOException e) { flag = false; System.out.println("接受消息失敗"); clients.remove(this); e.printStackTrace(); } if (din != null) { try { din.close(); } catch (IOException e) { System.out.println("din關(guān)閉失敗"); e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { System.out.println("din關(guān)閉失敗"); e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { System.out.println("din關(guān)閉失敗"); e.printStackTrace(); } } } /** * 功能:轉(zhuǎn)發(fā)給所有客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午11:11:59 * * @param message * @throws IOException */ private void forwordToAllClients(String message) throws IOException { for (Client c : clients) { if (c != this) { out = c.socket.getOutputStream(); dos = new DataOutputStream(out); forwordToClient(message); } } } /** * 功能:發(fā)送給一個(gè)客戶端 學(xué)校:山東師范大學(xué) 程序員:外力_Victor 日期:2016年5月8日 上午11:16:12 * * @throws IOException */ private void forwordToClient(String message) throws IOException { dos.writeUTF(message); dos.flush(); System.out.println("轉(zhuǎn)發(fā)成功!"); } } }
源碼下載:java聊天室代碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Java和WebSocket實(shí)現(xiàn)網(wǎng)頁聊天室實(shí)例代碼
- java socket實(shí)現(xiàn)聊天室 java實(shí)現(xiàn)多人聊天功能
- Java基于socket實(shí)現(xiàn)簡(jiǎn)易聊天室實(shí)例
- java實(shí)現(xiàn)一個(gè)簡(jiǎn)單TCPSocket聊天室功能分享
- 基于java編寫局域網(wǎng)多人聊天室
- Java Socket聊天室編程(一)之利用socket實(shí)現(xiàn)聊天之消息推送
- Java基于UDP協(xié)議實(shí)現(xiàn)簡(jiǎn)單的聊天室程序
- Java Socket聊天室編程(二)之利用socket實(shí)現(xiàn)單聊聊天室
- 使用java基于pushlet和bootstrap實(shí)現(xiàn)的簡(jiǎn)單聊天室
- Java編寫實(shí)現(xiàn)多人聊天室
相關(guān)文章
使用spring boot開發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問題
這篇文章主要介紹了使用spring boot開發(fā)時(shí)java對(duì)象和Json對(duì)象轉(zhuǎn)換的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03基于Java+SSM實(shí)現(xiàn)電影院購票系統(tǒng)
今天小編給大家?guī)硪豢頢SM的電影院售票系統(tǒng),非常不錯(cuò)的一個(gè)項(xiàng)目,是學(xué)習(xí)?javaweb編程必備。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-04-04java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別
這篇文章主要介紹了java中Statement 與 PreparedStatement接口之間的關(guān)系和區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java中equals()知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于Java中equals()知識(shí)點(diǎn)總結(jié)內(nèi)容,需要的朋友們可以學(xué)習(xí)參考下。2020-03-03java多線程之wait(),notify(),notifyAll()的詳解分析
本篇文章是對(duì)java多線程 wait(),notify(),notifyAll()進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06spring+maven實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了spring+maven實(shí)現(xiàn)發(fā)送郵件功能,利用spring提供的郵件工具來發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07java調(diào)用webservice接口,并解析返回參數(shù)問題
這篇文章主要介紹了java調(diào)用webservice接口,并解析返回參數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07