• 多线程聊天室


    基于Java的多线程聊天室

    客户端:

    public class MultiThreadClient {
    
        private static class RecMsg implements Runnable{
            private Socket client;
    
            public RecMsg(Socket client) {
                this.client = client;
            }
    
            @Override
            public void run() {
                Scanner sc = null;
                try {
                    sc = new Scanner(client.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(true){
                    if(client.isClosed()){
                        System.out.println("客户端退出");
                        break;
                    }
                    if(sc.hasNextLine()) {
                        System.out.println("服务端说:" + sc.nextLine());
                    }
                }
                sc.close();
            }
        }
    
    
        private static class SendMsg implements  Runnable{
            private Socket client;
    
            public SendMsg(Socket client) {
                this.client = client;
            }
    
            @Override
            public void run() {
                Scanner sc = new Scanner(System.in);
                PrintStream ps = null;
                try {
                    ps = new PrintStream(client.getOutputStream(),true,"UTF-8");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(true){
                    System.out.println("请输入内容");
                    String str = sc.nextLine();
                    ps.println(str);
                    if(str.contains("bye")){
                        break;
                    }
                }
                ps.close();
                sc.close();
    
            }
        }
    
        public static void main(String[] args) throws IOException {
            Socket sc = new Socket("127.0.0.1",8888);
            new Thread(new RecMsg(sc)).start();
            new Thread(new SendMsg(sc)).start();
        }
    }

    服务端:

    public class MultiThreadServer {
        //保存所有连接的客户端
        private static Map<String, Socket> clientMap = new ConcurrentHashMap<>();
    
        private static class ExecuteClientMap implements Runnable{
    
            private Socket client;
    
            public ExecuteClientMap(Socket client) {
                this.client = client;
            }
    
            @Override
            public void run() {
                Scanner sc = null;
                try {
                    sc = new Scanner(client.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(true){
                    if(sc.hasNextLine()){
                        String msgFromClient = sc.nextLine();
                        if(msgFromClient.startsWith("R:")){
                            //注册
                            //用户名
                            String username = msgFromClient.split(":")[1];
                            userRegister(username,client);
                        }else if(msgFromClient.startsWith("P:")){
                            //私聊P:用户名-信息
                            String userName = msgFromClient.split(":")[1].split("-")[0];
                            String msg = msgFromClient.split("-")[1];
                            sendPriUser(userName,msg);
    
                        }else if(msgFromClient.startsWith("G:")){
                            //群聊
                            String groupMsg = msgFromClient.split(":")[1];
                            group(groupMsg);
                        }
                    }
                }
            }
    
            private void sendPriUser(String userName,String msg) {
                Socket sc = clientMap.get(userName);
                PrintStream ps = null;
                try {
                    ps = new PrintStream(sc.getOutputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ps.println("私聊信息为:"+msg);
                ps.close();
                try {
                    sc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            private void userRegister(String username, Socket client) {
                clientMap.put(username,client);
                String str = username+"上线了";
                group(str);
            }
    
            private void group(String str) {
                Collection<Socket> values = clientMap.values();
                for(Socket sc : values){
                    PrintStream ps = null;
                    try {
                        ps = new PrintStream(sc.getOutputStream());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    ps.println(str);
    
                }
    
            }
        }
    
        public static void main(String[] args) throws IOException {
            ServerSocket ss = new ServerSocket(8888);
            ExecutorService executorService = Executors.newFixedThreadPool(20);
            for(int i = 0;i<20;i++){
                System.out.println("等待客户端连接");
                Socket sc = ss.accept();
                System.out.println("连接成功,端口号为:"+sc.getPort());
                //新建一个线程处理客户端的连接
                executorService.submit(new ExecuteClientMap(sc));
            }
            ss.close();
    
        }
    }
  • 相关阅读:
    HDU2647(拓扑排序+反向建图)
    Android Activity之间通信
    Swift 编程语言学习0.1——Swift简单介绍
    HDU 5012 Dice (BFS)
    当向后台插入或读取JSON数据遇见回车时
    Android CTS測试Fail项改动总结(四)
    【Unity 3D】学习笔记三十五:游戏实例——摄像机切换镜头
    android蓝牙4.0(BLE)开发之ibeacon初步
    Error opening zip file or JAR manifest missing : D:play-1.2.5/framework/play-1.2.5.jar
    Codeforces Round #256 (Div. 2)——Multiplication Table
  • 原文地址:https://www.cnblogs.com/du001011/p/11111783.html
Copyright © 2020-2023  润新知