溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何實(shí)現(xiàn)局域網(wǎng)聊天小程序

發(fā)布時(shí)間:2022-05-20 09:15:16 來(lái)源:億速云 閱讀:156 作者:zzz 欄目:開(kāi)發(fā)技術(shù)

今天小編給大家分享一下Java如何實(shí)現(xiàn)局域網(wǎng)聊天小程序的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來(lái)了解一下吧。

開(kāi)發(fā)環(huán)境:

IDEA 2018.2 集成開(kāi)發(fā)工具。

實(shí)現(xiàn)功能:

1、用戶上線,向服務(wù)器通知并注冊(cè)。

2、同局域網(wǎng)下,所有注冊(cè)用戶可以進(jìn)行群聊。

3、同局域網(wǎng)下,所有用戶可與任意已注冊(cè)用戶進(jìn)行私聊。

4、用戶下線,通知服務(wù)器,服務(wù)器更新信息。

實(shí)現(xiàn)原理:

1、服務(wù)器端實(shí)例化一個(gè)ServerSocket對(duì)象,調(diào)用accept方法等待客戶端連接到服務(wù)器。

2、客戶端實(shí)例化 Socket 對(duì)象,并使用構(gòu)造方法與服務(wù)器建立鏈接。

3、服務(wù)器端根據(jù)客戶端輸入信息,辨別客戶端請(qǐng)求的功能從而做出相應(yīng)響應(yīng)。

實(shí)用技術(shù):

為了能夠高效的處理客戶端的請(qǐng)求,在服務(wù)器端使用多線程處理客戶端請(qǐng)求。并且使用 ConcurrentHashMap 來(lái)存儲(chǔ)所有注冊(cè)過(guò)的客戶端。

項(xiàng)目源碼(解釋寫(xiě)在代碼的注釋當(dāng)中):

服務(wù)器端:

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ManyThreadServer {
    //存儲(chǔ)所有注冊(cè)的客戶端
    private static Map<String, Socket> clientMap = new ConcurrentHashMap<String,Socket>();
    //具體的處理每個(gè)客戶端的請(qǐng)求
    private static class ExcuteClient implements Runnable{
        private Socket client;
        public ExcuteClient(Socket client) {
            this.client = client;
        }
        @Override
        public void run() {
            try {
                //獲取客戶端的輸出流,讀取客戶端消息,并處理
                Scanner in = new Scanner(client.getInputStream());
                String strFromClient;
                while(true){
                    if(in.hasNextLine()){
                        strFromClient = in.nextLine();
                        //在Windows下默認(rèn)換行是:\r\n,所以把\r要轉(zhuǎn)換為空字符串
                        Pattern pattern = Pattern.compile("\r");
                        Matcher matcher = pattern.matcher(strFromClient);
                        strFromClient = matcher.replaceAll("");
                        //注冊(cè)流程
                        if(strFromClient.startsWith("useName")){
                            String useName = strFromClient.split("\\:")[1];
                            registerUser(useName,client);
                            continue;
                        }
                        //群聊功能
                        if(strFromClient.startsWith("G")){
                            String msg = strFromClient.split("\\:")[1];
                            groupChat(msg,client);
                            continue;
                        }
                        //私聊功能
                        if(strFromClient.startsWith("P")){
                            String userName = strFromClient.split("\\:")[1].split("-")[0];
                            String msg = strFromClient.split("\\:")[1].split("-")[1];
                            privateChat(userName,msg,client);
                            continue;
                        }
                        //用戶退出
                        if(strFromClient.startsWith("B")){
                            String userName = null;
                            //根據(jù)Socket找到UserName
                            for(String keyName : clientMap.keySet()){
                                if(clientMap.get(keyName).equals(client)){
                                    userName = keyName;
                                }
                            }
                            System.out.println("用戶" + userName + "下線了。。。");
                            clientMap.remove(userName);
                            System.out.println("當(dāng)前共有用戶" + clientMap.size() + "人");
                            continue;
                        }
                        else{
                            PrintStream out = new PrintStream(client.getOutputStream(),true,"UTF-8");
                            out.println("輸入錯(cuò)誤。。。");
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        private void registerUser(String name,Socket client){
            System.out.println("用戶:" + name + "已上線!");
            clientMap.put(name,client);
            System.out.println("當(dāng)前在線人數(shù):" + clientMap.size() + "人!");
            //既然是用戶在注冊(cè),所以這里服務(wù)器通知用戶注冊(cè)結(jié)果
            try {
                PrintStream out = new PrintStream(client.getOutputStream(),true,"UTF-8");
                out.println("用戶注冊(cè)成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        private void groupChat(String msg,Socket client){
            //取出clientMap中所有的Entry對(duì)象,遍歷每個(gè)用戶,并且發(fā)送消息
            Set<Map.Entry<String,Socket>> clientSet = clientMap.entrySet();
            for(Map.Entry<String,Socket> entry:clientSet){
                try {
                    Socket socket = entry.getValue();
                    //取得輸出流,向客戶端發(fā)送消息
                    PrintStream out = new PrintStream(socket.getOutputStream(),true,"UTF-8");
                    out.println("由端口號(hào)為"+ client.getPort() + "發(fā)來(lái)的群聊消息:" + msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        private void privateChat(String userName,String msg,Socket client){
            Socket privateSocket = clientMap.get(userName);
            try {
                PrintStream out = new PrintStream(privateSocket.getOutputStream(),true,"UTF-8");
                out.println("由端口號(hào)為:" + client.getPort() + "發(fā)來(lái)的消息:" + msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args)throws Exception{
        //為了提高效率,這里使用多線程進(jìn)行處理
        ExecutorService executorService = Executors.newFixedThreadPool(30);
        //實(shí)例化ServerSocket對(duì)象,并指定IP為本地主機(jī),端口號(hào)為6666
        ServerSocket serverSocket = new ServerSocket(6666);
        for(int i = 0; i < 30;i++){
            System.out.println("等待用戶連接。。。");
            //等待客戶端連接服務(wù)器
            Socket client = serverSocket.accept();
            System.out.println("有客戶端連接,端口號(hào)為:" + client.getPort());
            //啟動(dòng)線程,并處理客戶端請(qǐng)求
            executorService.submit(new ExcuteClient(client));
        }
        //關(guān)閉線程,關(guān)閉服務(wù)器
        executorService.shutdown();
        serverSocket.close();
    }
}

客戶端:

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
 
/**
 *  接收服務(wù)端發(fā)來(lái)的消息
 */
class FromServer implements Runnable{
    Socket client;
    public FromServer(Socket client){
        this.client = client;
    }
    @Override
    public void run() {
        try {
            Scanner in = new Scanner(client.getInputStream());
            while (true) {
                if (in.hasNextLine()) {
                    System.out.println("服務(wù)器:" + in.nextLine());
                }
                //判斷客戶端是否退出,如果推出,跳出循環(huán),并關(guān)閉流
                if (client.isClosed()) {
                    System.out.println("客戶端關(guān)閉。。。");
                    break;
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
/**
 *  向服務(wù)端發(fā)出消息
 */
class ToServer  implements Runnable{
    Socket client;
    public ToServer(Socket client){
        this.client = client;
    }
    @Override
    public void run() {
        try {
            Scanner scanner = new Scanner(System.in);
            PrintStream out = new PrintStream(client.getOutputStream(),true,"UTF-8");
            while (true) {
                System.out.println("請(qǐng)輸入信息:");
                String strToserver;
                if(scanner.hasNextLine()){
                    strToserver = scanner.nextLine().trim();
                    out.println(strToserver);
                    //客戶端退出標(biāo)志:B
                    if(strToserver.startsWith("B")){
                        System.out.println("客戶端退出。。。");
                        scanner.close();
                        out.close();
                        client.close();
                        break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class ManyThreadClient {
    public static void main(String[] args){
        try {
            //實(shí)例化Socket對(duì)象,與服務(wù)器建立連接
            Socket client = new Socket("127.0.0.1",6666);
            //為了發(fā)送消息和接收消息可以同時(shí)進(jìn)行,使用多線程進(jìn)行處理
            Thread thread1 = new Thread(new FromServer(client));
            Thread thread2 = new Thread(new ToServer(client));
            thread1.start();
            thread2.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

以上就是“Java如何實(shí)現(xiàn)局域網(wǎng)聊天小程序”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI