Java編寫實現(xiàn)多人聊天室
本文實例為大家分享了Java實現(xiàn)多人聊天室的具體代碼,供大家參考,具體內(nèi)容如下
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.服務(wù)端(寫了個內(nèi)部類:負(fù)責(zé)接收與發(fā)送多進(jìn)程)
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("歡迎進(jìn)入聊天室");
?? ??? ??? ?senOthers(this.name + "進(jìn)入了聊天室");
?? ??? ?} 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ā)送與接收多進(jìn)程
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.效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Java和WebSocket實現(xiàn)網(wǎng)頁聊天室實例代碼
- java socket實現(xiàn)聊天室 java實現(xiàn)多人聊天功能
- java聊天室的實現(xiàn)代碼
- Java基于socket實現(xiàn)簡易聊天室實例
- java實現(xiàn)一個簡單TCPSocket聊天室功能分享
- 基于java編寫局域網(wǎng)多人聊天室
- Java Socket聊天室編程(一)之利用socket實現(xiàn)聊天之消息推送
- Java基于UDP協(xié)議實現(xiàn)簡單的聊天室程序
- Java Socket聊天室編程(二)之利用socket實現(xiàn)單聊聊天室
- 使用java基于pushlet和bootstrap實現(xiàn)的簡單聊天室
相關(guān)文章
Spring中Websocket身份驗證和授權(quán)的實現(xiàn)
在Web應(yīng)用開發(fā)中,安全一直是非常重要的一個方面,本文主要介紹了Spring中Websocket身份驗證和授權(quán)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2023-08-08
tio-boot框架整合ehcache實現(xiàn)過程示例
這篇文章主要為大家介紹了tio-boot框架整合ehcache實現(xiàn)過程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Eclipse的Debug調(diào)試技巧大全(總結(jié))
這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
關(guān)于Springboot+gateway整合依賴并處理依賴沖突問題
這篇文章主要介紹了Springboot+gateway整合依賴并處理依賴沖突問題,給大家提到了spring boot版本和spring cloud版本,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01
SpringBoot集成Druid配置(yaml版本配置文件)詳解
這篇文章主要介紹了SpringBoot集成Druid配置(yaml版本配置文件),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

