IO模型

作者: Zeppelin421 | 来源:发表于2023-03-05 14:04 被阅读0次

Socket

Socket(套接字)就是两台主机之间逻辑连接的端点。

TCP/IP 协议是传输层协议,主要解决数据如何在网络中传输,而 HTTP 是应用层协议,主要解决如何包装数据。Socket 是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。它是网络通信过程中端点的抽象表示,包含进行网络通信必须得五种信息:连接使用的协议、本地主机的IP地址、本地进程的协议端口、远程主机的IP地址、远程进程的协议端口。

Socket流程

Socket 编程主要涉及客户端和服务端。服务端创建一个套接字(ServerSocket),并附加到一个端口上,服务器从这个端口监听连接。端口范围0~65536。客户端请求与服务器连接的时候,根据服务器的域名或IP地址加上端口号打开一个套接字。当服务器接受连接后,服务器和客户端之间的通信就像输入输出流一样进行操作。

Server:

    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("server started");
        while (true) {
            Socket accept = serverSocket.accept();
            executorService.execute(() -> handle(accept));
        }
    }

    public static void handle(Socket socket) {
        System.out.println("Thread ID:" + Thread.currentThread().getId() 
                         + " Thread Name:" + Thread.currentThread().getName());
        try {
            InputStream in = socket.getInputStream();
            byte[] bytes = new byte[1024];
            int read = in.read(bytes);
            System.out.println("Request: " + new String(bytes, 0, read));

            OutputStream os = socket.getOutputStream();
            os.write("Response: ".getBytes());
            os.write(bytes, 0, read);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

Client:

    public static void main(String[] args) {
        try {
            while (true) {
                Socket socket = new Socket("127.0.0.1", 9999);
                OutputStream os = socket.getOutputStream();
                System.out.println("input please");
                Scanner scanner = new Scanner(System.in);
                String msg = scanner.nextLine();
                os.write(msg.getBytes());

                InputStream is = socket.getInputStream();
                byte[] bytes = new byte[1024];
                int read = is.read(bytes);
                System.out.println(new String(bytes, 0, read));

                socket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

相关文章

  • 1.Nette入门第一章——IO演进

    1. IO 基础 1.1. linux网络IO模型 阻塞IO模型 非阻塞IO模型 IO多路复用模型(NIO) 信...

  • 细谈Select,Poll,Epoll

    阻塞 io 模型 blocking IO非阻塞 io 模型 nonblocking IOio多路复用模型 IO m...

  • 2.五种IO模型

    0.IO介绍1.阻塞IO模型2.非阻塞IO模型3.IO多路复用模型4.信号驱动IO模型5.异步IO模型6.五种IO...

  • 网络IO模型

    网络IO的模型大致包括下面几种 同步模型(synchronous IO)阻塞IO(bloking IO)非阻塞IO...

  • 「基础知识总结」 IO模型

    IO模型 五种IO模型包括:阻塞IO、非阻塞IO、信号驱动IO、IO多路转接、异步IO。其中,前四个被称为同步IO...

  • 五种 IO 模型

    五种 IO 模型 参考链接 一共有五种 IO 模型 阻塞 IO 非阻塞 IO 多路复用 IO 信号驱动 IO 异步...

  • Java中的IO模型

    Java中的IO模型 Java中的IO模型有四种: 同步阻塞IO 同步非阻塞IO IO多路复用 异步IO 其中IO...

  • 23、pythonIO模型

    python之路——IO模型 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步、异步、阻塞、非阻塞 ...

  • IO模型

    IO模型 五种模型

  • 2018-07-03

    Netty实践与NIO原理 一、阻塞IO与非阻塞IO Linux网络IO模型(5种) (1)阻塞IO模型 所有文件...

网友评论

    本文标题:IO模型

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