socket通信实现TCP协议的同步通信

实现tcp通信,一般第一想到的是用netty框架,但是netty实现的通信一般是异步,发送消息后,不需要等到回复。

最近遇到一个需求时,与某个网关进行tcp通信,发送请求消息之后会立马回复,并且不同的请求命令,返回的回复数据内容也不相同。思考再三,决定不使用netty,使用socket进行通信,请求后等到回复,收到回复内容进行解析。


import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.HexUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TCPClient {
   

    public static Map<String, Socket> ipChannelMap = new ConcurrentHashMap<String, Socket>();

    public static void main(String[] args) {
   
        try {
   
            //连接服务器
            Socket socket = new Socket("192.168.11.11", 8889);
            //发送指令
            String request = "12313112";
            OutputStream out = socket.getOutputStream();
            out.write(HexUtil.decode(request));
            out.flush();
            //接收服务器返回指令
            InputStream in = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int len = in.read(buffer);
            String response = new String(buffer, 0, len);
            String hexString = HexUtil.encodeToString(response);
            System.out.println("Client received: " + hexString);
            //断开服务器
            socket.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

    /**
    * @Title createSocket
    * @Description 建立socket连接
    * @param ip
    * @param port
    * @Author ju
    * @Return java.net.Socket
    * @Date: 2023/12/22 14:26
    */
    public static Socket createSocket(String ip,int port){
   
        //连接服务器
        Socket socket = null;
        try {
   
            socket = ipChannelMap.get(ip + ":" + String.valueOf(port));
            if (Func.isNull(socket)){
   
                socket = new Socket(ip, port);
                ipChannelMap.put(ip + ":" + String.valueOf(port),socket);
            }
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        return socket;
    }

    /**
    * @Title destroyConnect
    * @Description 断开socket连接
    * @param socket
    * @Author ju
    * @Return void
    * @Date: 2023/12/22 14:27
    */
    public static void destroyConnect(Socket socket){
   
        try {
   
            socket.close();
        } catch (IOException e) {
   
            e.printStackTrace();
        }
    }

    /**
    * @Title send
    * @Description 发送请求指令并返回服务器的返回指令
    * @param socket
    * @param request
    * @Author ju
    * @Return java.lang.String
    * @Date: 2023/12/22 14:27
    */
    public static String send(Socket socket,String request){
   
        String response = "";
        try {
   
            OutputStream out = socket.getOutputStream();
            out.write(HexUtil.decode(request));
            out.flush();
            //接收服务器返回指令
            InputStream in = socket.getInputStream(   );
            byte[] buffer = new byte[1024];
            int len = in.read(buffer);
            String str = new String(buffer, 0, len);
            response = HexUtil.encodeToString(str);
            System.out.println("Client received: " + response);
        } catch (IOException e) {
   
            e.printStackTrace();
        }
        return response;
    }
}

相关推荐

  1. socket通信实现TCP协议同步通信

    2024-01-10 08:40:04       56 阅读
  2. 基于TCP协议socket通信过程

    2024-01-10 08:40:04       46 阅读
  3. socket编程实现TCP通信

    2024-01-10 08:40:04       40 阅读
  4. 【Python】基于socket函数TCP通信

    2024-01-10 08:40:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-10 08:40:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-10 08:40:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-10 08:40:04       82 阅读
  4. Python语言-面向对象

    2024-01-10 08:40:04       91 阅读

热门阅读

  1. 关于数据库切换的麻烦

    2024-01-10 08:40:04       58 阅读
  2. Mysql in查询优化

    2024-01-10 08:40:04       54 阅读
  3. 正则表达式

    2024-01-10 08:40:04       43 阅读
  4. Tomcat服务实例部署

    2024-01-10 08:40:04       44 阅读
  5. ES6 新增 Set、Map 两种数据结构的理解

    2024-01-10 08:40:04       49 阅读
  6. LeetCode_4_困难_寻找两个正序数组的中位数

    2024-01-10 08:40:04       52 阅读
  7. 一、数据结构

    2024-01-10 08:40:04       58 阅读
  8. 抽丝剥茧设计模式-目录

    2024-01-10 08:40:04       68 阅读
  9. PHP数组复习

    2024-01-10 08:40:04       52 阅读
  10. Spring Boot参数校验方案

    2024-01-10 08:40:04       60 阅读
  11. Spring boot 启动添加访问地址和swagger地址输出

    2024-01-10 08:40:04       54 阅读
  12. Oracle游标深入探讨

    2024-01-10 08:40:04       61 阅读
  13. LeetCode 32. 最长有效括号

    2024-01-10 08:40:04       46 阅读