2、实现服务端消息群发的功能

服务器

 static List<Socket> socketList = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        // 单独启动一个线程   实现消息群发
        new Thread(new MsgAllSend(socketList)).start();

        while (true) {
            Socket socket = serverSocket.accept(); // 等待客户端连接
            socketList.add(socket); // 将新连接的socket存入集合
            // 为每一个socket启动专有线程 为之服务 读取消息
            new Thread(new ReadMsgThread(socket)).start();
        }
    }
 // 读取&展示消息的线程
    static class ReadMsgThread implements Runnable {
        private Socket socket ;
        public ReadMsgThread(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            try {
                boolean flag = true;
                while (true) {
                    // 接收成员发送的消息
                    //获取输入流  读取消息
                    InputStream in = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int read = in.read(bytes);
                    //  张三&你好
                    String msg = new String(bytes , 0 ,read);
                    // 解析
                    String[] split = msg.split("&");
                    String nickName = split[0]; // 昵称
                    String content = split[1]; // 要发送的内容
                    if (flag){
                        System.out.println(nickName + "上线了...");
                        flag = false;
                    }
                    System.out.println(nickName + "说:" + content);
                }
            } catch (Exception e) {
//                e.printStackTrace();
            }
        }
    }


    //  消息群发的线程
    static class MsgAllSend implements Runnable{

        private List<Socket> socketList;

        public MsgAllSend(List<Socket> socketList) {
            this.socketList = socketList;
        }

        @Override
        public void run() {
            try{
                Scanner sc = new Scanner(System.in);
                while (true){
                    System.out.println("请输入您要群发的内容:");
                    String msg = sc.next() ;
                    // 遍历socketList 获取集合中每一个socket
                    for (Socket socket : socketList){
                        OutputStream out = socket.getOutputStream();
                        out.write(msg.getBytes());
                    }
                }
            }catch (Exception e){

            }
        }
    }

客户端

public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 9999);
        // 启动接收   服务端  群发消息的线程
        new Thread( new ReadServerMsg(socket)).start();

        Scanner sc = new Scanner(System.in);
        System.out.println("请设置您的昵称:");
        String nickName = sc.next() ;
        while (true){
            OutputStream out = socket.getOutputStream();
            System.out.println("请输入您要发送的内容:");
            String content = sc.next();
            // 打包信息 昵称 与 内容  使用 & 连接
            // 张三&你好
            String msg = nickName + "&" + content ;
            out.write(msg.getBytes());
        }


    }

    static class ReadServerMsg implements Runnable{

        private Socket socket ;

        public ReadServerMsg(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try{
                while (true){
                    InputStream in = socket.getInputStream();
                    byte[] bytes = new byte[1024];
                    int read = in.read(bytes);
                    String msg = new String(bytes , 0 , read);
                    System.out.println("server:" + msg);
                }
            }catch (Exception e ){

            }
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值