Spingboot请求tcp 方式

avatar
作者
猴君
阅读量:0
 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();         }     } } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!