Java實(shí)現(xiàn)多人聊天室的原理與源碼
多人聊天室原理圖

源碼
工具類:
該類用于關(guān)閉各種流。
public class CloseUtil {
public static void CloseAll(Closeable... closeable){
for(Closeable c:closeable){
if (c != null) {
try {
c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服務(wù)器:
服務(wù)器端創(chuàng)建一個(gè)serverSocket對(duì)象通過accept()方法監(jiān)聽是否有tcp連接,同時(shí)有一個(gè)儲(chǔ)存socket對(duì)象的集合將連接進(jìn)來的對(duì)象儲(chǔ)存到List集合中,服務(wù)器將消息進(jìn)行轉(zhuǎn)發(fā)。
//服務(wù)器
public class Server {
//存儲(chǔ)每一個(gè)連接進(jìn)來的客戶端
public static List<MyChannel> list=new ArrayList<>();
public static void main(String[] args) throws Exception {
//創(chuàng)建ServerSocket對(duì)象
ServerSocket serverSocket = new ServerSocket(9999);
while (true){
//連接進(jìn)來的客戶端
Socket client = serverSocket.accept();
System.out.println(client.getInetAddress()+"進(jìn)入聊天室");
MyChannel myChannel = new MyChannel(client);
list.add(myChannel);
new Thread(myChannel).start();
}
}
}
消息轉(zhuǎn)發(fā)類:
具體的消息轉(zhuǎn)發(fā)實(shí)現(xiàn)類,將信息發(fā)給除發(fā)送消息以外的其他客戶端。
//用于信息轉(zhuǎn)發(fā)
public class MyChannel implements Runnable{
private DataInputStream dis;
private DataOutputStream dos;
private boolean flag=true;
public MyChannel(Socket socket) {
try{
dis=new DataInputStream(socket.getInputStream());
dos=new DataOutputStream(socket.getOutputStream());
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(dis,dos);
}
}
//接收數(shù)據(jù)的方法
private String receive(){
String str="";
try{
str= dis.readUTF();
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(dis,dos);
Server.list.remove(this);
}
return str;
}
//發(fā)送數(shù)據(jù)的方法
private void send(String str){
try {
if (str != null && str.length() != 0) {
dos.writeUTF(str);
dos.flush();
}
}catch (Exception exception){
flag=false;
CloseUtil.CloseAll(dos,dis);
Server.list.remove(this);
}
}
//轉(zhuǎn)發(fā)消息的方法
private void sendToOther(){
String str=this.receive();
List<MyChannel> list = Server.list;
for (MyChannel other:list) {
if(other==list){
continue;//不發(fā)送信息給自己
}
//將消息發(fā)送給其他客戶端
other.send(str);
}
}
@Override
public void run() {
while (flag){
sendToOther();
}
}
}
發(fā)送信息類:
用于從鍵盤上獲取數(shù)據(jù)然后將數(shù)據(jù)發(fā)送出去
public class Send implements Runnable{
//從鍵盤上獲取數(shù)據(jù)
private BufferedReader br;
private DataOutputStream dos;
private boolean flag=true;
public Send() {
br=new BufferedReader(new InputStreamReader(System.in));
}
public Send(Socket socket){
this();
try{
dos=new DataOutputStream(socket.getOutputStream());
}catch (Exception e){
flag=false;
CloseUtil.CloseAll(dos,socket);
e.printStackTrace();
}
}
private String getMessage(){
String str="";
try{
str=br.readLine();
}catch (IOException e){
flag=false;
CloseUtil.CloseAll(br);
}
return str;
}
private void send(String str){
try {
dos.writeUTF(str);
dos.flush();
} catch (IOException e) {
flag=false;
CloseUtil.CloseAll(dos);
e.printStackTrace();
}
}
@Override
public void run() {
while (flag){
this.send(getMessage());
}
}
}
信息接收類:
public class Receive implements Runnable{
//接受數(shù)據(jù)流
private DataInputStream dis;
private boolean flag=true;
public Receive(Socket socket){
try {
dis = new DataInputStream(socket.getInputStream());
}catch (Exception e){
flag=false;
CloseUtil.CloseAll(dis,socket);
}
}
private String getMessage(){
String str="";
try {
str=dis.readUTF();
} catch (IOException e) {
flag=false;
CloseUtil.CloseAll(dis);
e.printStackTrace();
}
return str;
}
@Override
public void run() {
while (flag){
System.out.println(this.getMessage());
}
}
}
客戶端:
public class client {
public static void main(String[] args) throws Exception{
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
Send send = new Send(socket);
Receive receive = new Receive(socket);
new Thread(send).start();
new Thread(receive).start();
}
}
先將服務(wù)器啟動(dòng)然后啟動(dòng)客戶端:測(cè)試結(jié)果如下

有喜歡的小伙伴可以自己拿去玩,代碼復(fù)制直接有效。
總結(jié)
到此這篇關(guān)于Java實(shí)現(xiàn)多人聊天室的原理與源碼的文章就介紹到這了,更多相關(guān)Java多人聊天室內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java socket實(shí)現(xiàn)聊天室 java實(shí)現(xiàn)多人聊天功能
- 基于java編寫局域網(wǎng)多人聊天室
- java編程實(shí)現(xiàn)多人聊天室功能
- Java基于Tcp/ip連接的多人交互聊天室
- java使用MulticastSocket實(shí)現(xiàn)基于廣播的多人聊天室
- Java SE實(shí)現(xiàn)多人聊天室功能
- Java NIO Selector用法詳解【含多人聊天室實(shí)例】
- Java基于中介者模式實(shí)現(xiàn)多人聊天室功能示例
- Java多線程實(shí)現(xiàn)多人聊天室功能
- java控制臺(tái)輸出版多人聊天室
相關(guān)文章
SpringBoot3.x打包Docker容器的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot3.x打包Docker容器的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法
這篇文章主要介紹了Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
基于Log4j2阻塞業(yè)務(wù)線程引發(fā)的思考
這篇文章主要介紹了基于Log4j2阻塞業(yè)務(wù)線程引發(fā)的思考,基于很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java啟用Azure Linux虛擬機(jī)診斷設(shè)置
這篇文章主要介紹了Java啟用Azure Linux虛擬機(jī)診斷設(shè)置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
java byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava byte數(shù)組與int,long,short,byte的轉(zhuǎn)換實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
淺談java多態(tài)的實(shí)現(xiàn)主要體現(xiàn)在哪些方面
下面小編就為大家?guī)硪黄獪\談java多態(tài)的實(shí)現(xiàn)主要體現(xiàn)在哪些方面。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09

