El cliente TCP (equipo a bordo del avión) se conecta al proxy inverso nginx para acceder al servidor TCP

Insertar descripción de la imagen aquíInsertar descripción de la imagen aquí

1. Enlace de solicitud TCP

Insertar descripción de la imagen aquí
①El cliente TCP se conecta al puerto nginx8667 y envía mensajes
②Nginx escucha el puerto 8667 y el proxy inverso transporta los parámetros de solicitud para acceder al servidor TCP
③Después de que el servidor TCP recibe la solicitud del cliente TCP, devuelve el resultado del procesamiento a través de la lógica empresarial
④nginx responde con el resultado devuelto por el servidor TCP al cliente TCP

2. Combate real
2.1 Exponer los puertos de servicio

Primero se inicia el servidor TCP y la consola de administración de Tencent Cloud expone el puerto TCP 1004.
Insertar descripción de la imagen aquí

2.2 TCP de proxy inverso de nginx

versión de demostración de nginx 1.22.0
configuración de nginx configuración de proxy inverso tcp

stream {
    
    
    server {
    
    
        listen 8667;
         proxy_pass tcp服务端IP:tpc服务端端口;
#          proxy_pass 101.xxx.xxx.80:10004;
    }
}

Consulte el apéndice al final del artículo para ver la configuración completa.

2.3 servidor tcp
package com.gblfy.socketclent;// 服务上不要有包名,本地测试可以有包名

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/**
 * tcp 服务端案例
 *
 * @author gblfy
 * @date 2023-08-03
 */
public class SocketService {
    
    

    public static void main(String[] args) throws IOException {
    
    
        // 此端口为服务端暴露的端口自定义,客户端连接时需要连接词端口
        ServerSocket ss = new ServerSocket(10004);
        Socket socket = ss.accept();
        String ip = socket.getInetAddress().getHostAddress();
        System.out.println("客户端的ip地址为," + ip);
        //1、获取Socket读取流
        BufferedReader bufin = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));


        HashMap<Object, Object> map = new HashMap<>();
        map.put("msg", "success");
        map.put("status", 200);

        //2、获取Socket输出流
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

        String line = null;
        while ((line = bufin.readLine()) != null) {
    
    
            System.out.println(line);
            // 3.服务端返回客户端消息
            out.println("服务端返回的-->" + JSON.toJSONString(map));
            // out.println("服务端返回的-->" + line.toUpperCase());
        }

        // 关闭连接
        socket.close();
        ss.close();
    }
}

2.4 Cliente TCP
package com.gblfy.socketclent;// 不要有包名

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * tcp 服务端案例
 *
 * @author gblfy
 * @date 2023-08-03
 */
public class SocketClient8667 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        //1、创建客户端Socket host 服务端IP地址 port 服务端端口
        Socket socket = new Socket("127.0.0.1", 10004);
        //2、获取终端输入的数据
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));


        //3、向服务端发送数据
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        // socket输入流,读取服务器返回的大写字母fias
        BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line = null;
        while ((line = buf.readLine()) != null) {
    
    
            // TODO 特殊场景 接收到某一个字符时,停止提供服务
            // if ("over".equals(line)) {
    
    
            //     break;
            // }

            out.println(line);
            //读取服务端大写的一行
            String upperStr = bufin.readLine();
            System.out.println(upperStr);
        }
        socket.close();
    }
}

2.5 Prueba de compilación de Linux

Se recomienda encontrar 2 servidores en la nube. La demostración utiliza Tencent Cloud Server
para compilar y ejecutar el código del servidor tcp y el código del cliente tcp en secuencia.

Compilar castaño:

javac tcp服务端.java
javac tcp客户端.java

Ejecutar castaño:

java tcp服务端
java tcp客户端
3. Uso interno
3.1 Caso del servidor
package com.gblfy.socketclent;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
public class SocketServiceApplication {
    
    

    public static void main(String[] args) throws IOException {
    
    
        SpringApplication.run(SocketServiceApplication.class, args);
        ServerSocket serverSocket = new ServerSocket(10004);
        while (true) {
    
    
            Socket socket = serverSocket.accept();
            new Thread(new ServerListener(socket)).start();

            System.out.println("启动TCP服务器....");
        }
    }
}
  • mensaje hexadecimal
package com.gblfy.socketclent;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;


/**
 * TCP 监听类接收客户端数据
 *
 * @author gblfy
 * @date 2023-08-04
 */
public class ServerListener implements Runnable {
    
    
    private Socket socket;

    ServerListener(Socket socket) {
    
    
        this.socket = socket;
    }

    @Override
    public void run() {
    
    
        try {
    
    
            String ip = socket.getInetAddress().getHostAddress();
            System.out.println("客户端的ip地址为," + ip);
            //1、获取Socket读取流
            BufferedReader bufin = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            //2、获取Socket输出流
            HashMap<Object, Object> map = new HashMap<>();
            map.put("msg", "success");
            map.put("status", 200);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            String line = null;
            while ((line = bufin.readLine()) != null) {
    
    
                // 单独处理逻辑类
                HandleService handleService = new HandleService();
                handleService.handle(line);

                // 3.TCP服务端返回消息
                out.println("服务端返回的-->" + JSON.toJSONString(map));
            }
            System.out.println("----------------" + line);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                socket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

package com.gblfy.socketclent;

import org.springframework.stereotype.Service;

/**
 * TCP 服务端逻辑处理类
 *
 * @author gblfy
 * @Date 2023-08-03
 **/
@Service
public class HandleService {
    
    

    public void handle(String msg) {
    
    
        //TODO 解析数据 落库
        System.out.println("------------" + msg);
    }
}

3.2 Servidor (hexadecimal)
  • mensaje hexadecimal
package com.gblfy.socketclent;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

@SpringBootApplication
public class SocketServiceApplication {
    
    

    public static void main(String[] args) throws IOException {
    
    
        SpringApplication.run(SocketServiceApplication.class, args);
        ServerSocket serverSocket = new ServerSocket(10004);
        while (true) {
    
    
            Socket socket = serverSocket.accept();
            new Thread(new ServerListener(socket)).start();

            System.out.println("启动TCP服务器....");
        }
    }
}
package com.gblfy.socketclent;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;


/**
 * TCP Server 监听类接收客户端(飞机机载设备)数据发送的十六进制数据
 *
 * @author gblfy
 * @date 2023-08-04
 */
public class ServerListener implements Runnable {
    
    
    private Socket socket;

    ServerListener(Socket socket) {
    
    
        this.socket = socket;
    }

    @Override
    public void run() {
    
    
        try {
    
    
            String ip = socket.getInetAddress().getHostAddress();
            System.out.println("客户端的ip地址为," + ip);

            //1、获取Socket读取流
            BufferedReader bufin = null;

            bufin = new BufferedReader(
                    new InputStreamReader(socket.getInputStream()));

            //2、获取Socket输出流
            HashMap<Object, Object> map = new HashMap<>();
            map.put("msg", "success");
            map.put("status", 200);

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            StringBuilder asciiOutput = new StringBuilder();
            System.out.println("开始返回");

            int line ;
            char[] cbuf = new char[1024];

            while ((line = bufin.read(cbuf)) != -1) {
    
    

                String str = new String(cbuf, 0, line);

                for (int i = 0; i < str.length(); i += 2) {
    
    
                    // System.out.println("====接收原始消息====="+line);
                    String hexByte = str.substring(i, i + 2);
                    int asciiValue = Integer.parseInt(hexByte, 16);
                    char asciiChar = (char) asciiValue;
                    asciiOutput.append(asciiChar);
                    // System.out.println("====接收转换消息====="+asciiOutput);
                }
                System.out.println("Start return");
                HandleService handleService = new HandleService();
                handleService.handle(asciiOutput.toString());
                asciiOutput.setLength(0);
                out.println("Server return json msg-->" + JSON.toJSONString(map));
            }
            System.out.println("=====接收消息====="+asciiOutput);

        } catch (Exception e) {
    
    
            System.out.println(e.getMessage());
            System.out.println("----------------catch");
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                socket.close();
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

package com.gblfy.socketclent;

import org.springframework.stereotype.Service;

/**
 * TCP 服务端逻辑处理类
 *
 * @author gblfy
 * @Date 2023-08-03
 **/
@Service
public class HandleService {
    
    

    public void handle(String msg) {
    
    
        //TODO 解析数据 落库
        System.out.println("------------" + msg);
    }
}

3.3 Caso del cliente
package com.gblfy.socketclent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient10004 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        SocketClient("127.0.0.1", 10004, "111");
    }

    public static void SocketClient(String tcpServerIp, int tcpServerPort, String requestParam) throws IOException {
    
    
        //1、创建客户端Socket
        Socket socket = new Socket(tcpServerIp, tcpServerPort);
        //2、向服务端发送数据
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
        out.println(requestParam);

        BufferedReader bufin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String res = bufin.readLine();
        System.out.println("------------>" + res);
        socket.close();
    }
}

3.4. nginx.conf
user  root;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    
    
    worker_connections  1024;
}


http {
    
    
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    # 加载/etc/nginx/conf.d/目录下以*.conf结尾的配置文件
    include /etc/nginx/conf.d/*.conf;
}
stream {
    
    
    server {
    
    
        listen 8667;
         proxy_pass tcp服务端IP:tpc服务端端口;
#          proxy_pass 101.xxx.xxx.80:10004;
    }
}

3.5 Depuración

Descripción: Asistente de depuración de red->Simulación de equipos aerotransportados
Insertar descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/weixin_40816738/article/details/132081204
Recomendado
Clasificación