网络通信(群聊)

作者: 晨曦_hero | 来源:发表于2017-09-24 00:27 被阅读2次

    // 发送者;消息的内容;接受者(协议)
    //多线程的使用
    //什么是协议
    // 服务端 (群聊)
    public class Selever {
    //存储socket 名字:socket
    static ArrayList<Socket> list = new ArrayList<Socket>();
    public static void main(String[] args) throws Exception {
    // 创建服务器socket
    ServerSocket server = new ServerSocket(8080);
    System.out.println("服务器等待接收中。。。。");
    // 接收到客户端socket

        while (true) {
            Socket socket = server.accept();//会阻塞
            System.out.println("服务器接受到客户端请求.....");
            //开启线程,接受socket的输入
            SeverListRunnable list = new SeverListRunnable();
            list.socket = socket;
            Thread read = new Thread(list);
            read.start();
    }
    

    }
    }

    class ServerRunnable implements Runnable {
    Scanner sc = new Scanner(System.in

    );
    Socket socket;
    
    @Override
    public void run() {
        // 发送消息
        // 给客户端回复
        OutputStream stream = null;
        try {
            while (true) {
                stream = socket.getOutputStream();
                // System.out.println("请输入:");
                String str1 = sc.next();
                stream.write(str1.getBytes());
            }
    
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    
    }
    

    }
    //接受用户的输入
    class SeverListRunnable implements Runnable{
    Socket socket ;
    @Override
    public void run() {
    try {
    InputStream stream = socket.getInputStream();
    while (true) {
    byte[] by = new byte[1024];
    int length = stream.read(by);
    String str = new String(by, 0, length);
    //1.谁发送的2.发送的消息3.发送给谁
    //解析字符串
    String[] arr = str.split(";");
    //发送者
    String from = arr[0];
    //消息
    String msg = arr[1];
    //接受者
    String to = arr[2];
    //判断list里是否包含这个人
    if (!(Selever.list.contains(from))) {
    Selever.list.add(socket);
    }
    for (Socket item : Selever.list) {
    OutputStream out = item.getOutputStream();
    out.write(str.getBytes());
    }
    }
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("io流异常");
    }
    }
    }

    public class Client {
    //客户端

        public static void main(String[] args) throws Exception {
            Socket socket = new Socket("172.18.26.191", 8080);
    
            ClientRunnable run = new ClientRunnable();
            run.socket = socket;
            Thread thread = new Thread(run);
            thread.start();
    
            // 客户端读出服务器回复的消息
            InputStream in = socket.getInputStream();
            while (true) {
                byte[] by = new byte[1024];
                int length = in.read(by);
                String str1 = new String(by, 0, length);
                String[] arr = str1.split(";");
                String from = arr[0];
                String msg = arr[1];
                String to = arr[2];
                System.out.println(from+":"+msg);
                System.out.println("服务器回复的消息:" + str1);
            }
        }
    }
    class ClientRunnable implements Runnable {
        Scanner sc = new Scanner(System.in);
        Socket socket;
    
        @Override
        public void run() {
    
            try {
                // 客户端端口号 写出请求
                OutputStream stream = socket.getOutputStream();
                while (true) {
                    System.out.println("客户端输入(from;msg;to)");
                    String str = sc.next();
                    stream.write(str.getBytes());
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:网络通信(群聊)

        本文链接:https://www.haomeiwen.com/subject/wswxextx.html