socket简单的实现群聊

1.群聊的简单思路实现
socket简单的实现群聊
2.群聊的代码实现:

客户端:

/**
* @des 客户端
*                  一个读的子线程 
*                  一个写的子线程
*/
public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		//创建 Socket 
		Socket socket = new Socket("localhost", 9999);
		//启动线程
		// 发送的子线程 
		new Thread(new Send(socket)).start();
		// 接收的子线程 
		new Thread(new Revice(socket)).start();
	}
}

public class IOUitls {
	/**
	 * 泛型方法 流的关闭
	 * @param arr
	 */
	public static <T extends AutoCloseable> void closeAll(T ... arr) {
		for (T t : arr) {
			if (t != null) {
				try {
					t.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	} 
}

public class Revice implements Runnable{
	//数据流 接收数据  
	private DataInputStream dis = null;
	private boolean isRunning = true;
	public Revice(Socket socket) {
		try {
			// 通过 socket 初始化流的操作 
			dis = new DataInputStream(socket.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
			//关闭流 
			IOUitls.closeAll(dis,socket);
			isRunning = false;
		}
	}
	/**
	 * 接收数据
	 * @return
	 */
	public String revice() {
		String msg = null;
		try {
			// 读取 字符串 
			msg = dis.readUTF();
		} catch (IOException e) {
			e.printStackTrace();
			// 关闭流
			IOUitls.closeAll(dis);
			isRunning = false;
		}
		return msg;
	}
	@Override
	public void run() {
		//循环的接收数据 
		while (isRunning) {
			System.out.println(revice());
		}
	}
}

public class Send implements Runnable {
	private Scanner sc = null;
	private DataOutputStream dos  = null;
	private boolean isRunning = true;
	public Send(Socket socket) {
		try {
			this.dos = new DataOutputStream(socket.getOutputStream());
			sc = new Scanner(System.in);
		} catch (IOException e) {
			e.printStackTrace();
			IOUitls.closeAll(dos,socket);
			isRunning = false;
		}
	}
	/**
	 * 发送数据
	 */
	public void send() {
		//获取键盘的输入 
		System.out.println("请输入:");
		String msg = sc.next();
		//发送到服务器端 
		try {
			dos.writeUTF(msg);
			dos.flush();
		} catch (IOException e) {
			e.printStackTrace();	
			IOUitls.closeAll(dos);
		}
	}
	@Override
	public void run() {
		while (isRunning) {
			send();
		}
	}
}

服务端代码简单实现:

/**
 * 群聊服务器的实现
 */
public class Server {

    // 服务器 管理集合 的连接通道
    private List<Channel> list = new ArrayList<>();
    public static void main(String[] args) {
        try {
            new Server().start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 启动服务器
     */
    private void start() throws IOException {
        ServerSocket socket = new ServerSocket(9999);
        while (true){
            Socket client = socket.accept();
            //来个一个客户端  建立了 通道
            Channel channel = new Channel(client);
            //  添加到集合中进行管理
            list.add(channel);
            //启动线程
            new Thread(channel).start();
        }
    }
    /**
     * 连接通道的类
     */
    class Channel implements Runnable {
        // 2 个流
        private DataInputStream dis = null;
        private DataOutputStream dos = null;

        //传入一个socket 进来
        public Channel(Socket socket) {
            try {
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**
         * 接收数据
         * @return
         */
        public String revice() {
            String msg = null;
            try {
                msg = dis.readUTF();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return msg;
        }
        /**
         * 发送数据
         * @param msg
         */
        public void send(String msg){
            if (msg == null || msg == "" ){
                return;
            }
            try {
                dos.writeUTF(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        /**
         *  给其他用户发送数据
         */
        public void sendOther(){
            //接收数据
            String msg = revice();
            //给其他用户发送书 循环发送
            for (Channel channel : list) {
                if (channel == this){ // 不给自己发送数据
                    continue;
                }
                channel.send(msg);
            }
        }
        @Override
        public void run() {
            while (true){
                sendOther();
            }
        }
    }
}