Java編寫實現(xiàn)多人聊天室
更新時間:2022年09月15日 16:46:59 作者:java_zhangwei
這篇文章主要為大家詳細介紹了Java編寫實現(xiàn)多人聊天室,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java實現(xiàn)多人聊天室的具體代碼,供大家參考,具體內容如下
1.客戶端
package tk.javazhangwei.net.tcp.chat.Demo03; ? import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.Socket; ? /*** ?* 創(chuàng)建客戶端 ?發(fā)送數(shù)據(jù)+接收數(shù)據(jù) ?*? ?* @author zw ?* ?*/ public class Client { ?? ?public static void main(String[] args) throws IOException { ?? ??? ?BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); ?? ??? ?System.out.println("請輸入一個喜歡的名稱:"); ?? ??? ?String name = bf.readLine(); ?? ??? ?if(name.equals("")) { ?? ??? ??? ?return; ?? ??? ?} ?? ??? ? ?? ??? ?Socket client = new Socket("localhost",1025); ?? ??? ?//控制臺輸入信息 ?? ??? ?//控制臺輸入信息 ?? ??? ?new Thread(new Send(client,name)).start();//一條路徑 ?? ??? ?new Thread(new Receive(client)).start();//一條路徑 } }
2.服務端(寫了個內部類:負責接收與發(fā)送多進程)
package tk.javazhangwei.net.tcp.chat.Demo03; ? import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.List; ? public class Server { ?? ?List<myChannel> all = new ArrayList<myChannel>(); ?? ? ?? ? ?? ? ?? ?public static void main(String[] args) throws IOException { ?? ??? ?new Server().start(); ?? ??? ? ? ?? ?} ?? ? ?? ? ?? ?public void start() throws IOException { ?? ??? ?ServerSocket server = new ServerSocket(1025); ?? ??? ?while (true) { ?? ??? ??? ?Socket socket = server.accept(); ?? ??? ??? ?myChannel mc = new myChannel(socket); ?? ??? ??? ?Thread t = new Thread(mc); ?? ??? ??? ?all.add(mc); ?? ??? ??? ?t.start(); ?? ?} } ? ? /*** ?* 一個客戶端 一個通路 ?* @author zw ?* ?*/ class myChannel implements Runnable{ ?? ?private DataInputStream dis; ?? ?private DataOutputStream dos; ?? ?private boolean isRuning=true; ?? ?private String name; ?? ? ?? ?public myChannel(Socket socket) throws IOException{ ?? ??? ?try { ?? ??? ??? ?dis = new DataInputStream(socket.getInputStream()); ?? ??? ??? ?dos = new DataOutputStream(socket.getOutputStream()); ?? ??? ??? ?this.name = dis.readUTF(); ?? ??? ??? ?send("歡迎進入聊天室"); ?? ??? ??? ?senOthers(this.name + "進入了聊天室"); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?//e.printStackTrace(); ?? ??? ??? ??? ?isRuning=false; ?? ??? ??? ??? ?dos.close(); ?? ??? ??? ??? ?dis.close(); ?? ??? ??? ??? ? ?? ??? ?} ?? ??? ? ?? ?} ?? ?/*** ?? ? * 讀取數(shù)據(jù) ?? ? *? ?? ? * @return ?? ? * @throws IOException ?? ? */ ?? ?private String receive() throws IOException { ?? ??? ?String msg =""; ?? ??? ?try { ?? ??? ??? ?msg =dis.readUTF(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?//e.printStackTrace(); ?? ??? ??? ?isRuning=false; ?? ??? ??? ?dis.close(); ?? ??? ? ?? ??? ?} ?? ??? ? ?? ??? ? ?? ??? ?return msg;?? ? ?? ?} ?? ?/*** ?? ? * 發(fā)送數(shù)據(jù) ?? ? * @throws IOException? ?? ? */ ?? ?private void send(String msg) throws IOException { ?? ??? ?if(msg==null&& msg.equals("")) { ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?try { ?? ??? ??? ?dos.writeUTF(msg); ?? ??? ??? ?dos.flush(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?//e.printStackTrace(); ?? ??? ??? ?isRuning=false; ?? ??? ??? ?dos.close(); ?? ??? ??? ?all.remove(this); ?? ??? ?} ?? ??? ? ?? ?} ?? ?/*** ?? ? * 發(fā)送給其他客戶端 ?? ? * @throws IOException? ?? ? */ ?? ?private void senOthers(String msg) throws IOException { ?? ??? ?if (msg.startsWith("@")&&msg.indexOf(":")>-1) {// 表示為私聊 ?? ??? ??? ?//獲取name ?? ??? ??? ?String name = msg.substring(1, msg.indexOf(":")); ?? ??? ??? ?String content = msg.substring(msg.indexOf(":")+1);//獲取冒號后的正文 ?? ??? ??? ?for (myChannel others : all) { ?? ??? ??? ??? ?if(others.name.equals(name)) { ?? ??? ??? ??? ??? ?others.send(this.name+"對您瞧瞧的說:"+content); ?? ??? ??? ??? ?} ?? ??? ??? ??? ? ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ??? ? ?? ??? ?} else { ?? ??? ?//遍歷容器 ?? ??? ?for(myChannel others:all) { ?? ??? ??? ?if(others == this) {//如果是本身,就跳過 ?? ??? ??? ??? ?continue; ?? ??? ??? ?} ?? ??? ??? ?others.send(this.name+"對所有人說:"+msg); ?? ??? ?} ?? ??? ?} ?? ?} ?? ? ?? ?@Override ?? ?public void run() { ?? ??? ?while(isRuning) { ?? ??? ??? ?try { ?? ??? ??? ??? ?senOthers(receive()) ; ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ??? ? ?? ??? ?} ?? ??? ?} } }
3.客戶端的發(fā)送與接收多進程
package tk.javazhangwei.net.tcp.chat.Demo03; ? import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; ? /*** ?* 發(fā)送數(shù)據(jù) ?*? ?* @author zw ?* ?*/ public class Send implements Runnable{ ?? ?//控制臺輸入流 ?? ?private BufferedReader console; ?? ?//管道輸出流 ?? ?private DataOutputStream dos; ?? ?private String name; ?? ? ?? ?private boolean isRuning =true;//線程是否運行 ?? ? ?? ?public Send() { ?? ??? ?console =new BufferedReader(new InputStreamReader(System.in)); ?? ?} ?? ? ?? ?public Send(Socket client,String name) throws IOException { ?? ??? ?this(); ?? ??? ?try { ?? ??? ??? ?dos = new DataOutputStream(client.getOutputStream()); ?? ??? ??? ?this.name = name; ?? ??? ??? ?send(name); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?//e.printStackTrace(); ?? ??? ??? ? isRuning =false; ?? ??? ??? ? dos.close(); ?? ??? ??? ? console.close(); ?? ??? ?} ?? ?} ?? ?private String getMsgFromConsole() { ?? ??? ?try { ?? ??? ??? ?return console.readLine(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ?return ""; ?? ?} ? ?? ? @Override ?? ?public void run() { ?? ??? ? ?? ??? ?while(isRuning) { ?? ??? ??? ?try { ?? ??? ??? ??? ?send(getMsgFromConsole()); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ? ?? ?public void send(String msg) throws IOException { ?? ??? ?if (msg!=null && !msg.equals("")) { ?? ??? ??? ?try { ?? ??? ??? ??? ?dos.writeUTF(msg); ?? ??? ??? ??? ?dos.flush();// 強制刷新 ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?// e.printStackTrace(); ?? ??? ??? ??? ?isRuning = false; ?? ??? ??? ??? ?dos.close(); ?? ??? ??? ??? ?console.close(); ? ?? ??? ??? ?} ?? ??? ?} ?? ?} ? }
package tk.javazhangwei.net.tcp.chat.Demo03; ? import java.io.DataInputStream; import java.io.IOException; import java.net.Socket; ? /*** ?* 接收數(shù)據(jù) ?* @author zw ?* ?*/ public class Receive implements Runnable{ ?? ?//客戶端的輸入流 ?? ?private DataInputStream dis; ?? ?private boolean isRuning = true; ?? ? ?? ?public Receive() { ?? ? ?? ?} ? ?? ?public Receive(Socket client) { ?? ??? ?super(); ?? ??? ?try { ?? ??? ??? ?dis = new DataInputStream(client.getInputStream()); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?//e.printStackTrace(); ?? ??? ??? ?isRuning =false; ?? ??? ??? ?try { ?? ??? ??? ??? ?dis.close(); ?? ??? ??? ?} catch (IOException e1) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e1.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?/*** ?? ? * 接收數(shù)據(jù) ?? ? * @return ?? ? * @throws IOException? ?? ? */ ?? ?public String receive() throws IOException { ?? ??? ?String msg = ""; ?? ??? ?try { ?? ??? ??? ?msg =dis.readUTF(); ?? ??? ?} catch (IOException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?isRuning =false; ?? ??? ??? ?dis.close(); ?? ??? ?} ?? ??? ? ?? ??? ?return msg; ?? ?} ?? ?@Override ?? ?public void run() { ?? ??? ?while(isRuning) { ?? ??? ??? ?try { ?? ??? ??? ??? ?System.out.println(receive()); ?? ??? ??? ?} catch (IOException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ??? ? ?? ?} ? }
4.效果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 使用Java和WebSocket實現(xiàn)網頁聊天室實例代碼
- java socket實現(xiàn)聊天室 java實現(xiàn)多人聊天功能
- java聊天室的實現(xiàn)代碼
- Java基于socket實現(xiàn)簡易聊天室實例
- java實現(xiàn)一個簡單TCPSocket聊天室功能分享
- 基于java編寫局域網多人聊天室
- Java Socket聊天室編程(一)之利用socket實現(xiàn)聊天之消息推送
- Java基于UDP協(xié)議實現(xiàn)簡單的聊天室程序
- Java Socket聊天室編程(二)之利用socket實現(xiàn)單聊聊天室
- 使用java基于pushlet和bootstrap實現(xiàn)的簡單聊天室
相關文章
java中Sources目錄Resources目錄的區(qū)別解讀
這篇文章主要介紹了java中Sources目錄Resources目錄的區(qū)別解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12Mybatis通用Mapper(tk.mybatis)的使用
本文主要介紹了Mybatis通用Mapper(tk.mybatis)的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07Idea入門教程之一分鐘創(chuàng)建一個Java工程
idea作為Java開發(fā)最好用的編寫代碼軟件之一,首先進行的就是工程的創(chuàng)建,這篇文章主要給大家介紹了關于Idea入門教程之一分鐘創(chuàng)建一個Java工程的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-07-07SpringMVC異步處理操作(Callable和DeferredResult)
這篇文章主要介紹了SpringMVC異步處理操作(Callable和DeferredResult),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01