网络协议

  1. http
  2. tcp
    1. 服务端
    2. 客户端

http

http :统一的标准,大家都可互相调用访问

rpc:自家公司中各个服务之间调用

https://www.zhihu.com/question/524580708/answer/3571952043

tcp

是一种面向连接的、可靠的、基于字节流的传输层通信协议。它是互联网协议套件(TCP/IP)中的核心协议之一,广泛应用于各种网络通信场景,如网页浏览、文件传输、电子邮件等。

服务端

import java.io.*;
import java.net.*;

public class TCPServer {
    public static void main(String[] args) {
        int port = 6789; // 服务器端口

        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("Server is listening on port " + port);

            while (true) {
                Socket socket = serverSocket.accept();
                System.out.println("New client connected");

                try (InputStream in = socket.getInputStream();
                     OutputStream out = socket.getOutputStream()) {

                    // 1. 读取客户端发送的数据
                    byte[] buffer = new byte[1024];
                    int bytesRead = in.read(buffer);
                    if (bytesRead > 0) {
                        String requestMessage = new String(buffer, 0, bytesRead);
                        System.out.println("Received from client: " + requestMessage);
                    }

                    // 2. 发送二进制响应数据给客户端
                    String responseMessage = "Hello, Client!";
                    byte[] responseData = responseMessage.getBytes(); // 将字符串转换为字节数组
                    out.write(responseData); // 发送二进制数据
                    System.out.println("Sent " + responseData.length + " bytes to client");

                } catch (IOException e) {
                    System.err.println("I/O error: " + e.getMessage());
                }
            }
        } catch (IOException e) {
            System.err.println("Could not listen on port " + port);
            e.printStackTrace();
        }
    }
}

客户端

接收二进制案例

import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String[] args) {
        String serverAddress = "127.0.0.1"; // 服务器地址
        int serverPort = 6789; // 服务器端口

        try (Socket socket = new Socket(serverAddress, serverPort);
             OutputStream out = socket.getOutputStream(); // 发送数据的输出流
             InputStream in = socket.getInputStream()) { // 接收数据的输入流

            // 1. 发送请求数据到服务器
            String requestMessage = "Hello, Server!";
            byte[] requestData = requestMessage.getBytes(); // 将字符串转换为字节数组
            out.write(requestData); // 发送数据
            System.out.println("Sent to server: " + requestMessage);

            // 2. 接收服务器的二进制响应数据
            byte[] buffer = new byte[1024]; // 接收数据的缓冲区
            // 实际读取的字节数
            int bytesRead; 
            // 用于存储接收到的数据
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

            // 循环读取数据,直到流结束
            while ((bytesRead = in.read(buffer)) != -1) {
                // 将读取的数据写入字节数组输出流
                byteArrayOutputStream.write(buffer, 0, bytesRead); 
            }

            // 3. 将接收到的二进制数据转换为字节数组
            byte[] responseData = byteArrayOutputStream.toByteArray();
            System.out.println("Received " + responseData.length + " bytes from server");

            // 4. 处理接收到的二进制数据(例如打印或解析)
            // 这里简单地将二进制数据转换为字符串并打印
            String responseMessage = new String(responseData);
            System.out.println("Received from server: " + responseMessage);

        } catch (UnknownHostException e) {
            System.err.println("Unknown host: " + serverAddress);
        } catch (IOException e) {
            System.err.println("I/O error: " + e.getMessage());
        }
    }
}

日夜颠倒头发少 ,单纯好骗恋爱脑 ,会背九九乘法表 ,下雨只会往家跑 ,搭讪只会说你好 ---- 2050781802@qq.com

×

喜欢就点赞,疼爱就打赏

相册 说点什么 简历