码迷,mamicode.com
首页 > 编程语言 > 详细

多线程聊天室

时间:2019-07-01 00:40:04      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:tac   tput   用户   map   shm   user   cli   用户名   close   

基于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();

    }
}

 

多线程聊天室

标签:tac   tput   用户   map   shm   user   cli   用户名   close   

原文地址:https://www.cnblogs.com/du001011/p/11111783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!