BIO servidor y cliente Comunicaciones

prefacio

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

una 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("==============================================");
    }
}

dos 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();
    }
}

resume III

1, la ventaja BIO

Escribir simple mirada.

2, deficiencias BIO

El bloqueo de IO, el servidor puede abrir procesamiento multi-hilo, sino la creación de hilos es limitado, dará lugar a la escasez de recursos, se bloquea el servicio,

También podemos abrir el grupo de subprocesos, para controlar el número de subprocesos creados, pero no lo apoyo concurrente, al responder a miles de cantidad concurrente,

aceptará un montón de bloqueo, no podemos apoyar alta concurrencia.

Publicados 502 artículos originales · ganado elogios 358 · Vistas 1,18 millones +

Supongo que te gusta

Origin blog.csdn.net/yhl_jxy/article/details/104565810
Recomendado
Clasificación