One-stop learning Java network programming comprehensive understanding of BIO_NIO_AIO, learning notes (3)

Hello everyone, my 方圆
knowledge here is pretty simple


1. BIO blocking model

Insert picture description here
Briefly describe the response process of the server and the client in the BIO model

  1. The server serverSocketmust first and 端口proceed绑定
  2. After the binding is completed, execute accept方法and wait for the client to connect. This method is 阻塞式调用, that is, to wait for the client's connection response, do nothing, wait forever, (the blocked are InputStream.read(), OutputStream. write(), these two will always wait for the client's response)
  3. The client creates Socketan object, 绑定the server ip地址and 端口号, with the server connection
  4. The server receives the client's connection request, the accept method gets it 客户端的socket信息, and the connection is successful
  5. Server and client create their own io流, realize全双工通信
  6. Anytime afterwards结束连接

2. Simple practical demonstration

2.1 Server

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


public class Server {
    
    
    public static void main(String[] args) {
    
    
        final int DEFAULT_PORT = 8888;
        final String QUIT = "quit";
        ServerSocket serverSocket = null;

        try {
    
    
            //绑定端口号
            serverSocket = new ServerSocket(DEFAULT_PORT);
            System.out.println("服务器已经启动,绑定端口号:" + DEFAULT_PORT);

            while (true){
    
    
                //等待客户端的连接
                Socket socket = serverSocket.accept();
                System.out.println("客户端" + socket.getPort() + ":" + "已经连接");

                //获取io流
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(socket.getInputStream())
                );
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(socket.getOutputStream())
                );

                //读取客户发送的信息
                String msg = null;
                while ((msg = reader.readLine()) != null) {
    
    
                    System.out.println("客户端" + socket.getPort() + ":" + msg);
                    //回复消息
                    writer.write( msg + " ok" +"\n");
                    writer.flush();
                    System.out.println("服务器:" + msg + " ok");

                    if(msg.equals(QUIT)){
    
    
                        System.out.println("客户端" + socket.getPort() + ":断开连接" );
                        break;
                    }
                }
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            if(serverSocket != null){
    
    
                try {
    
    
                    serverSocket.close();
                    System.out.println("服务器Socket关闭");
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

2.2 Client



import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    
    
    public static void main(String[] args) {
    
    
        final int DEFAULT_SERVER_PORT = 8888;
        final String DEFAULT_ADDRESS = "127.0.0.1";
        final String QUIT = "quit";

        Socket socket = null;
        BufferedWriter writer = null;

        try {
    
    
            //创建Socket
            socket = new Socket(DEFAULT_ADDRESS,DEFAULT_SERVER_PORT);

            //创建io流
            writer = new BufferedWriter(
                    new OutputStreamWriter(socket.getOutputStream())
            );
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(socket.getInputStream())
            );

            //等待用户输入信息
            while (true) {
    
    
                BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
                String msg = consoleReader.readLine();
                //向服务器发送消息
                writer.write(msg + "\n");
                writer.flush();
                System.out.println("客户端"+ ":" + msg);
                String line = reader.readLine();
                System.out.println("服务器:" + line);
                //退出判断
                if(msg.equals(QUIT)){
    
    
                    break;
                }
            }
        } catch (UnknownHostException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            if(writer != null){
    
    
                try {
    
    
                    writer.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }

    }
}

2.3 Response results

  • Client
    Insert picture description here
  • server
    Insert picture description here

3. Deepen understanding

Insert picture description here


Hurry up and order!

Guess you like

Origin blog.csdn.net/qq_46225886/article/details/107469924