简单模拟聊天

本文介绍了一个简易聊天服务器的实现过程,包括服务器端和客户端代码。服务器端使用了ServerSocket监听指定端口,通过ExecutorService线程池处理客户端连接请求,并利用ConcurrentHashMap管理已连接的客户端。客户端通过Socket连接服务器,接收并显示服务器广播的消息。

服务器端:代码展示

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);   //创建服务器端口号
        ExecutorService service = Executors.newCachedThreadPool();  //创建线程池
        ConcurrentHashMap<Socket, SocketAddress> hashMap = new ConcurrentHashMap<>();
        System.out.println("等待服务器连接。。。");
        while (true) {
            Socket accept = ss.accept();
            hashMap.put(accept, accept.getRemoteSocketAddress());//等待客户端连接
            service.submit(() -> {          //把所要执行的代码放到线程池中执行
                try {
                    byte[] bytes = new byte[1024];        //创建字节数组
                    InputStream inputStream = accept.getInputStream();//服务器读入客户端所传来的信息
                    SocketAddress address = accept.getRemoteSocketAddress();
                    while (true) {
                        int read = inputStream.read(bytes); //把读入的信息放到字节数组中
                        if (read == -1) {      //如果读到-1则结束读的过程
                            break;
                        }
                        String s = address + "" + new String(bytes, 0, read);  //把读入字节数组的信息转化成字符串并且打印出来
                        //System.out.println(s);
                        for (Socket sd : hashMap.keySet()) {
                            OutputStream outputStream = sd.getOutputStream();
                            outputStream.write(s.getBytes());
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            });
        }

    }
}

客户端:代码展示:

public class Demo {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 8888);   //创建客户端
        Scanner sc = new Scanner(System.in);    //创建输入流
        new Thread(() -> {
            InputStream inputStream = null;
            try {
                inputStream = socket.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            while (true) {
                byte[] bytes = new byte[1024];
                int read = 0;
                try {
                    read = inputStream.read(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (read == -1) {
                    break;
                }
                String string = new String(bytes, 0, read);
                System.out.println(string);

            }
        }).start();
        while (true) {
            String s = sc.nextLine();       //从控制台输入,并且发送给服务器端
            socket.getOutputStream().write(s.getBytes());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值