BIO Server和Client通信

前言

Github:https://github.com/yihonglei/thinking-in-netty

一 SocketServer

package com.jpeony.netty.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 1、bio单线程时接受客户端请求慢;
 * 2、bio多线程时并发量高会创建大量线程,系统线程开启数量有限,系统资源不够崩掉,
 * 你可以开线程池处理,但是当访问了剧增时,处理不了成千上万的并发量;
 * 3、bio支撑不了高并发访问
 *
 * @author yihonglei
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8989);
        while (true) {
            System.out.println("等待客户端连接......");
            // 阻塞方法,等待客户端连接
            Socket socket = serverSocket.accept();
            System.out.println("有客户端连接了......");
            // 数据处理
            handler(socket);
            // 可以使用多线程处理,缺陷就是并发量高时会创建大量线程,系统线程开启数量有限,系统资源不够崩掉
//            new Thread(() -> {
//                try {
//                    handler(socket);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }).start();
        }
    }

    private static void handler(Socket socket) throws IOException {
        System.out.println("thread id:" + Thread.currentThread().getId());
        byte[] bytes = new byte[1024];

        System.out.println("准备read......");
        // 接收客户端的数据,阻塞方法,没有数据可读时就阻塞
        int read = socket.getInputStream().read(bytes);
        System.out.println("read完毕!");
        if (read != -1) {
            System.out.println("接收到客户端的数据:" + new String(bytes, 0, read));
            System.out.println("thread id:" + Thread.currentThread().getId());

        }
        socket.getOutputStream().write("Hello Client!".getBytes());
        socket.getOutputStream().flush();
        System.out.println("==============================================");
    }
}

二 SocketClient

package com.jpeony.netty.bio;

import java.io.IOException;
import java.net.Socket;

/**
 * @author yihonglei
 */
public class SocketClient {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8989);
        // 向服务端发送数据
        socket.getOutputStream().write("Hello Server!".getBytes());
        socket.getOutputStream().flush();
        System.out.println("向服务端发送数据结束!");
        byte[] bytes = new byte[1024];
        // 接收服务端响应数据
        int read = socket.getInputStream().read(bytes);
        System.out.println("接收服务端响应数据:" + new String(bytes, 0, read));
        socket.close();
    }
}

三 总结

1、BIO优点

编写简单,一目了然。

2、BIO缺点

阻塞IO,服务端可以开启多线程处理,但是线程的创建也是有限的,会造成资源紧缺,服务崩溃,

我们也可以开启线程池处理,控制线程的创建数量,但是支持不了搞并发,当应对成千上万的并发量时,

accept将会大量阻塞,支持不了高并发。

发布了502 篇原创文章 · 获赞 358 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/yhl_jxy/article/details/104565810
BIO