Spingboot请求tcp 方式
创始人
2024-11-15 02:04:13
 import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service;  import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel;   /**  * 请求tcp接口  *  * @author Mr丶s  * @date 2024/7/10 下午3:03  * @description  */ @Slf4j @Service public class TcpClientUtils {      private SocketChannel socketChannel;       /**      * 创建通道      *      * @param host      * @param port      * @return      */     public String connect(String host, int port) {         try {             if (socketChannel == null || !socketChannel.isConnected() || !socketChannel.isOpen()) {                 socketChannel = SocketChannel.open();                 socketChannel.connect(new InetSocketAddress(host, port));                 return "Connection successful";             } else {                 return "Already connected";             }         } catch (IOException e) {             e.printStackTrace();             return "Connection failed: " + e.getMessage();         }     }      /**      * 关闭通道      */     public void closeConnection() {         try {             if (socketChannel != null && socketChannel.isOpen()) {                 socketChannel.close();             }         } catch (IOException e) {             e.printStackTrace();         }     }      /**      * 发送消息      *      * @param host      * @param port      * @param message      * @return      */     public String sendMessage(String host, int port, String message) {         String response = "";          try {             if (socketChannel == null || !socketChannel.isConnected() || !socketChannel.isOpen()) {                 String connectionStatus = connect(host, port);                 if (!"Connection successful".equals(connectionStatus)) {                     return "Failed to connect: " + connectionStatus;                 }             }              // 发送数据             ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());             socketChannel.write(buffer);              // 读取响应             ByteBuffer responseBuffer = ByteBuffer.allocate(1024);             socketChannel.read(responseBuffer);             responseBuffer.flip();              // 输出响应数据             StringBuilder stringBuilder = new StringBuilder();             while (responseBuffer.hasRemaining()) {                 stringBuilder.append((char) responseBuffer.get());             }             response = stringBuilder.toString();             log.info("TCP 请求返回: " + response);         } catch (IOException e) {             e.printStackTrace();             response = "Failed to send message: " + e.getMessage();         }          return response;     }      public static void main(String[] args) {         try {             // 创建SocketChannel             SocketChannel socketChannel = SocketChannel.open();             // 连接服务器             socketChannel.connect(new InetSocketAddress("47.114.51.90", 18888));             // 发送数据             // String message = "P*Hello, Server!*B*K\n";             String message = "P*1*55*21*240321002*1*0*8*0*0*0*1722496736654*1*240314002*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*0*B*K\n";             ByteBuffer buffer = ByteBuffer.wrap(message.getBytes());             socketChannel.write(buffer);             // 读取响应             ByteBuffer responseBuffer = ByteBuffer.allocate(1024);             socketChannel.read(responseBuffer);             responseBuffer.flip();             // 输出响应数据             while (responseBuffer.hasRemaining()) {                 System.out.print((char) responseBuffer.get());             }             // 关闭连接             socketChannel.close();         } catch (IOException e) {             e.printStackTrace();         }     } } 

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...