设计C/S架构的IM通信软件(3)

在之前的功能基础上,可以区分开群聊和私聊

引入哈希表来实现,具体代码如下:

客户端类:

复制代码
public class MClient {
    //创建客户端 Socket 再获取输入输出流 启动线程不实时读取服务器发来的消息 String msg 再写一个发送消息 Scanner scan
    //写发送消息的方法 sendMsg (os) 写读取指定长度数据 readMsg()用到保存消息的缓冲区 byte[]
    public static void main(String[] args) throws IOException {
        new MClient().StartMClient();
    }
    public OutputStream os;
    public InputStream is;
    public void StartMClient() throws IOException {
        //创建客户端
        Socket socket = new Socket("127.0.0.1",9999);
        //获取输入输出流
        is = socket.getInputStream();
        os = socket.getOutputStream();
        //启动线程时不实时读取消息
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    String msg = null;
                    try {
                        msg = readMsg();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(msg);
                }
            }
        }).start();
        //发送消息
        while(true){
            Scanner scan = new Scanner(System.in);//从键盘获得输入
            String line = scan.nextLine();
            sendMsg(line);
        }
    }
    //读取消息
    public String readMsg() throws IOException {
        byte[] b = new byte[1024];
        is.read(b);
        String msg = new String(b);
        return msg.trim();
    }
    //发送消息
    public void sendMsg(String msg) throws IOException {
        String str = msg + "\r\n";
        os.write(str.getBytes());
        os.flush();
    }
}

服务器端:

复制代码
public class MServer {
    public static void main(String[] args) throws IOException {
        new MServer().StartMServer();
    }
    public void StartMServer() throws IOException {
        //创建一个服务器 用哈希表Map<Integer,Socket> map存用户客户端 给用户端设一个ID用来
        //连接客户端server.accept 并放到哈希表 启动线程 保持通信状态
        ServerSocket server = new ServerSocket(9999);
        Map<Integer, Socket> map = new HashMap<>();
        int UserID = 1;
        while(true){
            //连接客户端 把客户端添加到map中
            Socket socket = server.accept();
            map.put(UserID,socket);
            //启动线程 保持通信
            ServerThread serverThread = new ServerThread(socket,map,UserID++);
            new Thread(serverThread).start();
        }
    }
}

线程通信类:

复制代码
public class ServerThread implements Runnable {
    public Socket socket;
    public OutputStream os;
    public InputStream is;
    public Map<Integer,Socket> map;
    public int UserID;
    public ServerThread(Socket socket,Map<Integer,Socket> map,int UserID){
        this.socket = socket;
        this.map = map;
        this.UserID = UserID;
        try {
            os = socket.getOutputStream();
            is = socket.getInputStream();
            sendMsg(UserID,os,"客户端连接成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void run() {
        //读取msg 再去分析msg是广播还是私聊对msg进行分割
        while(true){
            try {
                String msg = readMsg();
                String[] msgArr = msg.split(":");
                if(msgArr[0].equals("g")){
                    //群发
                    for(Map.Entry<Integer,Socket> entry : map.entrySet()){
                        Socket s = entry.getValue();
                        if(s != socket){
                            OutputStream output = s.getOutputStream();
                            sendMsg(UserID,output,msg);
                        }
                    }
                }else{
                    //私聊
                    Integer id = Integer.parseInt(msgArr[0]);
                    //根据id找到对象
                    Socket s = map.get(id);
                    OutputStream output = s.getOutputStream();
                    sendMsg(UserID,output,msgArr[1]);
                }
                System.out.println("client : " + msg);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //读取消息
    public String readMsg() throws IOException {
        byte[] b = new byte[1024];
        is.read(b);
        String msg = new String(b);
        return msg.trim();
    }
    //发送消息
    public void sendMsg(int UserId,OutputStream os,String msg) throws IOException {
        String str =UserId + ":" + " " + msg + "\r\n";
        os.write(str.getBytes());
        os.flush();
    }
}
相关推荐
java1234_小锋5 分钟前
Java高频面试题:MyBatis如何实现动态数据源切换?
java·开发语言·mybatis
塔望品牌咨询9 分钟前
烙印营销的“系统工程”:从“散点式”到“系统式”的十要务架构
架构·消费品·消费战略·塔望消费战略·消费品战略
墨神谕13 分钟前
Java中,为什么要将.java文件编译成,class文件,而不是直接将.java编译成机器码
java·开发语言
毛骗导演18 分钟前
OpenClaw 沙箱执行系统深度解析:一条 exec 命令背后的安全长城
前端·架构
Nyarlathotep011327 分钟前
并行设计模式(3):Future模式
java·后端
流星雨在线29 分钟前
汇总:Tomcat 安装与常用配置
java·tomcat
小冷coding1 小时前
【面试】结合项目整理的场景面试题,覆盖 Java 基础、锁、多线程、数据库、分布式锁 / 事务、消息中间件等核心维度
java·数据库·面试
鬼先生_sir1 小时前
SpringCloud-GateWay网关
java·spring cloud·gateway
文心快码BaiduComate1 小时前
Comate搭载GLM-5.1:长程8H,对齐Opus 4.6
前端·后端·架构
毛骗导演1 小时前
OpenClaw Pi Agent 深度解析:嵌入式 Agent 运行时的架构设计与实现
前端·架构