聊天室控制,私聊选定一个线程

来源:互联网 发布:为什么选择软件测试 编辑:程序博客网 时间:2024/06/05 15:49

仅仅控制服务器端代码的修改,其他的代码接上一篇

package com.dasenlin.mutilatechartpromte;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.ArrayList;import java.util.List;/** * 服务端 * @author Administrator * */public class Server {    /**     * 多个客户端     */    private List<MyChannal>all =new ArrayList<MyChannal>();    /**     * 启动服务器     * @param args     */    public static void main(String[] args) {            try {                new Server().start();            } catch (IOException e) {                e.printStackTrace();            }    }    public void start() throws IOException{            while(true){                ServerSocket server = new ServerSocket(8888);                while(true){                    Socket client =server.accept();                    MyChannal channel = new MyChannal(client);                    all.add(channel);                    new Thread(channel).start();                }            }    }    /**     * 一条线路     * @author Administrator     *     */    private class MyChannal implements Runnable{        private DataInputStream dis;        private DataOutputStream dos;        private boolean isRunning =true;        private String name;        public MyChannal(Socket client){             try {                dis = new DataInputStream(client.getInputStream());                dos = new DataOutputStream(client.getOutputStream());                 this.name = dis.readUTF();                this.send("欢迎您进入多人聊天室");                sendOthers(this.name+"进入了聊天室",true);//添加所有人和私聊的区别            } catch (IOException e) {                IoCloserUtil.closeAll(dos,dis);                isRunning=false;            }        }        /**         * 接收消息         * @return         */        private String receive(){            String msg="";            try {                msg = dis.readUTF();            } catch (IOException e) {                IoCloserUtil.closeAll(dis);                isRunning=false;                all.remove(this);            }            return msg;        }        /**         * 发送消息         * @param msg         */        private void send(String msg){            if(null==msg||msg.equals(null)){                return ;            }            try {                dos.writeUTF(msg);                dos.flush();            } catch (IOException e) {                IoCloserUtil.closeAll(dos);                isRunning=false;                all.remove(this);            }        }        /**         * 发送给其他客户端         * 添加判断到是系统消息还是客户消息         */        private void sendOthers(String msg,boolean sys){            //是否为私聊            if(msg.startsWith("@")&&msg.indexOf(":")>-1){                String name = msg.substring(1, msg.indexOf(":"));                String content = msg.substring(msg.indexOf(":")+1);                for(MyChannal other :all){                    if(other.name.equals(name)){                       other.send(this.name+"敲敲的对你说:"+content);                    }                }            }else{                for(MyChannal other :all){                    if(other==this){                        continue;                    }                    if(sys){                     other.send("系统信息:"+msg);                    }else{                     other.send(this.name+"对所有人说:"+msg);                        }                }            }        }        /**         * 循环运行,发送给其他客户端         */        @Override        public void run() {            while(true){                sendOthers(receive(),false);            }        }    }}
0 0