4.BIO模式下的多个客户端

1.服务端代码:
    public class Server {
        public static void main(String[] args) {
            try {
                System.out.println("======服务端启动=======");
                //1.定义一个ServerSocket对象进行服务端的端口注册
                ServerSocket ss=new ServerSocket(9999);
                System.out.println("======服务端等待连接.......");
                while (true){
                    //2.监听客户端的Socket连接请求
                    Socket socket = ss.accept();
                    //重点1:创建一个线程处理一个连接请求
                    new Thread(new ServerThreadReader(socket)).start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

2.服务端线程
    public class ServerThreadReader implements Runnable{
        private Socket socket;

        public ServerThreadReader(Socket socket) {
            this.socket = socket;
        }

        public void run() {
            //3.从Socket管道中得到一个字节输入流对象
            InputStream inputStream= null;
            try {
                inputStream = socket.getInputStream();
                //4.把字节输入流包装称一个缓冲字符输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
                String msg;
                while ((msg = br.readLine()) != null) {
                    System.out.println("服务端线程:"+Thread.currentThread().getName()+"接收到的消息:" + msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

3.客户端线程
    public class Client {
        public static void main(String[] args) {
            try {
                //1.创建Socket对象请求服务端的连接
                Socket client = new Socket("127.0.0.1", 9999);
                //2.从Scoket对象中获取一个字节输出流
                OutputStream outputStream = client.getOutputStream();
                //3.把字节流包装成一个打印流
                PrintStream ps = new PrintStream(outputStream);
                Scanner scanner = new Scanner(System.in);
                while (true) {
                    System.out.print("客户端--->请讲:");
                    String msg = scanner.nextLine();
                    ps.println(msg);
                    ps.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

 

小结:

  1.每个socket连接,都会创建一个线程,线程的竞争、切换上下文影响性能

  2.每个线程都会占用栈空间和CPU资源

  3.并不是每个socket都进行IO操作,无意义的线程处理

  4.客户端的并发访问增加时。服务端讲呈现1:1的线程开销,访问量越大,系统将发生线程栈溢出,线程创建失败,最终导致进程宕机或者僵死,从而不能对外提供服务

posted @ 2022-08-04 22:28  努力的达子  阅读(50)  评论(0编辑  收藏  举报