当前位置: 首页 > news >正文

Spingboot请求tcp 方式


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 {// 创建SocketChannelSocketChannel 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();}}
}
http://www.lryc.cn/news/412395.html

相关文章:

  • leetcode刷题日记-括号生成
  • 小程序按钮分享
  • 多模态多智能体,在实现系统2(深思熟虑)方面的探索
  • 【CAN通讯系列8】如何准确接收数据?
  • RabbitMQ知识总结(基本概念)
  • Prel语言入门学习:一篇全面的指南
  • 在云服务器上自动化部署项目,jenkins和gitee
  • python 参数输入
  • Spring面试篇章——Spring基本概述
  • 股票预测模型中注意力多层Attention RNN LSTM 的应用
  • C语言 | Leetcode C语言题解之第313题超级丑数
  • PHP健身微信小程序系统源码
  • 树组件 el-tree 数据回显
  • 54、PHP 实现希尔排序
  • linux 虚拟机解压arm-linux-gcc-4.6.4-arm-x86_64.tar.bz2并arm-linux-gcc
  • 泛化的最近点迭代法(Generalized-ICP)
  • Java | Leetcode Java题解之第313题超级丑数
  • 单细胞数据整合-去除批次效应harmony和CCA (学习)
  • MuRF代码阅读
  • pycharm无法导入pyside2模块;“ModuleNotFoundError: No module named ‘PySide2“
  • c语言指针中“数组名的理解”以及“一维数组传参”的本质
  • 计算机毕业设计Python+Flask微博舆情分析 微博情感分析 微博爬虫 微博大数据 舆情监控系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
  • KubeBlocks v0.9 解读|最高可管理 10K 实例的 InstanceSet 是什么?
  • ZW3D二次开发_菜单_禁用/启用表单按钮
  • windows子系统wsl完成本地化设置locale,LC_ALL
  • MYSQL 根据条件order by 动态排序
  • DirectX修复工具下载安装指南:电脑dll修复拿下!6种dll缺失修复方法!
  • vue3(1)虚拟数字键盘的封装,(2)以及子组件改变父组件变量的值进而使子组件实时响应值的变化,(3)子组件调用父组件中的方法(带参)
  • 反序列化靶机serial
  • 扎克伯格说Meta训练Llama 4所需的计算能力是Llama 3的10倍