依赖

<dependency><groupId>org.t-io</groupId><artifactId>tio-core</artifactId><version>3.7.4.v20210808-RELEASE</version></dependency>

Packet

import cn.hutool.core.util.HexUtil;
import com.demo.socket.util.HexStrUtil;
import lombok.Getter;
import lombok.Setter;
import org.tio.core.intf.Packet;import java.io.UnsupportedEncodingException;/*** @author Lenovo 消息体*/
@Setter
@Getter
public class MindPackage extends Packet {private static final long serialVersionUID = -172060606924066412L;/*** 包头长度*/public static final Integer HEADER = 4;/*** 记录包长度的长度*/public static final Integer LENGTH = 4;/*** 登录ID长度*/public static final Integer LOGIN_ID = 4;/*** 命令码长度*/public static final Integer COMMAND_CODE = 2;/*** 命令ID长度*/public static final Integer COMMAND_ID = 4;/*** 命令ID长度 0正常1非正常;数据前 8 字节是错误码,之后是 UTF8 编码的错误信息*/public static final Integer RESULT = 1;/*** 加密方式0未加密长度*/public static final Integer SECRET = 1;/*** 压缩方式长度;0=无压缩,1=gzip;*/public static final Integer ZIP = 1;/*** 数据长度*/public static final Integer DATA = null;/*** 包尾长度*/public static final Integer LAST = 4;/*** 编码*/public static final String CHARSET = "utf-8";/*** 字节数组:包头*/private byte[] head = HexStrUtil.hexStrToBinaryStr("3C 43 6D 64");/*** 字节数组:包长度 【null都要初始化一下,否则发送心跳包计算长度的时候会空指针】*/private byte[] length = new byte[4];/*** 字节数组:登录ID*/private byte[] loginId = new byte[4];/*** 字节数组:命令码*/private byte[] commandCode = new byte[2];/*** 字节数组:命令ID 自定义*/private byte[] commandId = new byte[4];/*** 字节数组:访问结果*/private byte[] result = HexUtil.decodeHex("00");/*** 字节数组:加密方式*/private byte[] secret = HexStrUtil.hexStrToBinaryStr("00");/*** 字节数组:压缩方式*/private byte[] zip = HexStrUtil.hexStrToBinaryStr("00");/*** 字节数组:数据*/private byte[] data = new byte[0];/*** 字节数组:包尾*/private byte[] last = HexStrUtil.hexStrToBinaryStr("63 6D 44 3E");/*** 计算出包长度字节4byte*/public byte[] getReqLengthByCount() {int number = loginId.length+ commandCode.length+ commandId.length+ secret.length+ zip.length+ data.length;//先转16进制字符串,再转bytesbyte[] bytes = new byte[4];byte[] temp = HexUtil.decodeHex(Integer.toHexString(number));//固定占4个字节,所以这里找个容器转一道System.arraycopy(temp, 0, bytes, 0, temp.length);return bytes;}/*** 计算出包长度字节4byte*/public byte[] getResLengthByCount() {int number = loginId.length+ commandCode.length+ commandId.length+ secret.length+ result.length+ zip.length+ data.length;//先转16进制字符串,再转bytesbyte[] bytes = new byte[4];byte[] temp = HexUtil.decodeHex(Integer.toHexString(number));//固定占4个字节,所以这里找个容器转一道System.arraycopy(temp, 0, bytes, 0, temp.length);return bytes;}/*** 获取字符串数据*/public String getStringData() throws UnsupportedEncodingException {return new String(this.data, CHARSET);}/*** 设置字符串数据*/public void setStringData(String data) {//先转16进制字符串,再转bytesString data16 = HexUtil.encodeHexStr(data).toUpperCase();this.data = HexUtil.decodeHex(data16);}
}

客户端处理器实现

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.demo.socket.entity.Response;
import com.demo.socket.service.IResponseService;
import com.demo.socket.socketplus.Remark;
import com.demo.socket.util.HexStrUtil;
import org.tio.client.intf.ClientAioHandler;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;import java.nio.ByteBuffer;
import java.util.Date;/*** @author Lenovo 处理器*/
public class ClientAioHandlerImpl implements ClientAioHandler {/*** 创建心跳包*/@Overridepublic Packet heartbeatPacket(ChannelContext channelContext) {return new MindPackage();}/*** 根据ByteBuffer解码成业务需要的Packet对象.* 如果收到的数据不全,导致解码失败,请返回null,在下次消息来时框架层会自动续上前面的收到的数据*/@Overridepublic Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {MindPackage mindPackage = new MindPackage();//至少要知道包头和包长度if (readableLength < MindPackage.HEADER + MindPackage.LENGTH) {return null;}//包头bytebyte[] head = new byte[4];buffer.get(head, 0, MindPackage.HEADER);mindPackage.setHead(head);//包长度bytebyte[] length = new byte[4];buffer.get(length, 0, MindPackage.LENGTH);mindPackage.setLength(length);//位运算得出包长度int number = HexStrUtil.bytes2int(length);//一个完整的包的长度必须大于可读取的长度if (MindPackage.HEADER + MindPackage.LENGTH + number + MindPackage.LAST > readableLength) {return null;} else {//登录IDbyte[] loginId = new byte[4];buffer.get(loginId, 0, 4);mindPackage.setLoginId(loginId);//命令码byte[] commandCode = new byte[2];buffer.get(commandCode, 0, 2);mindPackage.setCommandCode(commandCode);//命令IDbyte[] commandID = new byte[4];buffer.get(commandID, 0, 4);mindPackage.setCommandId(commandID);//执行结果byte[] result = new byte[1];buffer.get(result, 0, 1);mindPackage.setResult(result);//加密方式byte[] secret = new byte[1];buffer.get(secret, 0, 1);mindPackage.setSecret(secret);//压缩方式byte[] zip = new byte[1];buffer.get(zip, 0, 1);mindPackage.setZip(zip);//数据int dataLength = number - MindPackage.HEADER- MindPackage.COMMAND_CODE- MindPackage.COMMAND_ID- MindPackage.RESULT- MindPackage.SECRET- MindPackage.ZIP;byte[] data = new byte[dataLength];buffer.get(data, 0, dataLength);mindPackage.setData(data);//包尾byte[] last = new byte[4];buffer.get(last, 0, 4);mindPackage.setLast(last);return mindPackage;}}/*** 编码*/@Overridepublic ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {MindPackage mindPackage = (MindPackage) packet;byte[] head = mindPackage.getHead();byte[] length = mindPackage.getReqLengthByCount();byte[] loginId = mindPackage.getLoginId();byte[] commandCode = mindPackage.getCommandCode();byte[] commandId = mindPackage.getCommandId();byte[] secret = mindPackage.getSecret();byte[] zip = mindPackage.getZip();byte[] data = mindPackage.getData();byte[] last = mindPackage.getLast();//组建在一起byte[] bytes = ArrayUtil.addAll(head, length, loginId, commandCode, commandId, secret, zip, data, last);ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);byteBuffer.order(tioConfig.getByteOrder());byteBuffer.put(bytes);return byteBuffer;}/*** 处理消息包*/@Overridepublic void handler(Packet packet, ChannelContext channelContext) throws Exception {System.out.println("------------------------------------------START--------------------------------------------------------");//处理解码后的消息MindPackage mindPackage = (MindPackage) packet;//登录if (Remark.LOGIN_COMMAND_ID.equals(HexUtil.encodeHexStr(mindPackage.getCommandId()))) {IResponseService responseService = SpringUtil.getBean(IResponseService.class);String hexStrCommandId = HexUtil.encodeHexStr(mindPackage.getCommandId());Response response = responseService.query(hexStrCommandId);System.out.println("result16" + HexUtil.encodeHexStr(mindPackage.getResult()));//是否登录成功if ("00".equals(HexUtil.encodeHexStr(mindPackage.getResult()))) {response.setJson(mindPackage.getStringData());response.setResponseTime(new Date());int i = responseService.updateData(response);System.out.println("试图保存登录结果response表:" + JSON.toJSONString(response) + ":::" + i);} else {response.setJson("{\"Result\":\"error\"}");response.setResponseTime(new Date());int i = responseService.updateData(response);System.out.println("试图保存登录错误结果response表:" + JSON.toJSONString(response) + ":::" + i);}System.out.println("登录接口结果:" + mindPackage.getStringData());} else {System.out.println("客户端最终捕获commandID:"+HexUtil.encodeHexStr(mindPackage.getCommandId()));}System.out.println("--------------------------------------------END--------------------------------------------------------");}
}

客户端监听器实现

import cn.hutool.core.util.HexUtil;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.intf.Packet;/*** @author Lenovo 监听器*/
public class ClientAioListenerImpl implements ClientAioListener {/*** 建链后触发本方法,注:建链不一定成功,需要关注参数isConnected** @param channelContext* @param isConnected    是否连接成功,true:表示连接成功,false:表示连接失败* @param isReconnect    是否是重连, true: 表示这是重新连接,false: 表示这是第一次连接* @throws Exception*/@Overridepublic void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect) throws Exception {//建立(重连)连接之后需要重新登录if (isConnected) {String message = "{\"Account\":\"Account\",\"Password\":\"Password\"}";MindPackage mindPackage = new MindPackage();mindPackage.setCommandId(HexUtil.decodeHex(Remark.LOGIN_COMMAND_ID));mindPackage.setCommandCode(HexUtil.decodeHex(Remark.LOGIN_COMMAND_CODE));mindPackage.setLoginId(HexUtil.decodeHex("00 00 00 00"));mindPackage.setStringData(message);//发出登录请求Tio.send(channelContext, mindPackage);}}/*** 原方法名:onAfterDecoded* 解码成功后触发本方法** @param channelContext* @param packet* @param packetSize* @throws Exception*/@Overridepublic void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize) throws Exception {}/*** 接收到TCP层传过来的数据后** @param channelContext* @param receivedBytes  本次接收了多少字节* @throws Exception*/@Overridepublic void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes) throws Exception {}/*** 消息包发送之后触发本方法** @param channelContext* @param packet* @param isSentSuccess  true:发送成功,false:发送失败* @throws Exception*/@Overridepublic void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess) throws Exception {}/*** 处理一个消息包后** @param channelContext* @param packet* @param cost           本次处理消息耗时,单位:毫秒* @throws Exception*/@Overridepublic void onAfterHandled(ChannelContext channelContext, Packet packet, long cost) throws Exception {}/*** 连接关闭前触发本方法** @param channelContext the channelcontext* @param throwable      the throwable 有可能为空* @param remark         the remark 有可能为空* @param isRemove* @throws Exception*/@Overridepublic void onBeforeClose(ChannelContext channelContext, Throwable throwable, String remark, boolean isRemove) throws Exception {}
}

服务端处理器

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.core.TioConfig;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;import java.nio.ByteBuffer;
import java.util.Date;
import java.util.UUID;/*** @author Lenovo 处理器*/
@Slf4j
public class ServerAioHandlerImpl implements ServerAioHandler {/*** 根据ByteBuffer解码成业务需要的Packet对象.* 如果收到的数据不全,导致解码失败,请返回null,在下次消息来时框架层会自动续上前面的收到的数据** @param buffer         参与本次希望解码的ByteBuffer* @param limit          ByteBuffer的limit* @param position       ByteBuffer的position,不一定是0哦* @param readableLength ByteBuffer参与本次解码的有效数据(= limit - position)* @param channelContext* @return* @throws*/@Overridepublic Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {if (readableLength == 1) {return null;}MindPackage mindPackage = new MindPackage();//至少要知道包头和包长度log.info("readableLength:" + readableLength);if (readableLength < MindPackage.HEADER + MindPackage.LENGTH) {log.info("readable太小无法解码");return null;}//包头bytebyte[] head = new byte[4];buffer.get(head, 0, MindPackage.HEADER);mindPackage.setHead(head);//包长度bytebyte[] length = new byte[4];buffer.get(length, 0, MindPackage.LENGTH);mindPackage.setLength(length);//位运算得出包长度int number = HexStrUtil.bytes2int(length);log.info("number:" + number);//一个完整的包的长度必须大于可读取的长度if (MindPackage.HEADER + MindPackage.LENGTH + number + MindPackage.LAST > readableLength) {log.info("包长大于readable,本次无法解码");return null;} else {//登录IDbyte[] loginId = new byte[4];buffer.get(loginId, 0, 4);mindPackage.setLoginId(loginId);//命令码byte[] commandCode = new byte[2];buffer.get(commandCode, 0, 2);mindPackage.setCommandCode(commandCode);//命令IDbyte[] commandID = new byte[4];buffer.get(commandID, 0, 4);mindPackage.setCommandId(commandID);//加密方式byte[] secret = new byte[1];buffer.get(secret, 0, 1);mindPackage.setSecret(secret);//压缩方式byte[] zip = new byte[1];buffer.get(zip, 0, 1);mindPackage.setZip(zip);//数据int dataLength = number - MindPackage.HEADER- MindPackage.COMMAND_CODE- MindPackage.COMMAND_ID- MindPackage.SECRET- MindPackage.ZIP;byte[] data = new byte[dataLength];buffer.get(data, 0, dataLength);mindPackage.setData(data);//包尾byte[] last = new byte[4];buffer.get(last, 0, 4);mindPackage.setLast(last);return mindPackage;}}/*** 编码** @param packet* @param tioConfig* @param channelContext* @return*/@Overridepublic ByteBuffer encode(Packet packet, TioConfig tioConfig, ChannelContext channelContext) {MindPackage mindPackage = (MindPackage) packet;byte[] head = mindPackage.getHead();byte[] length = mindPackage.getResLengthByCount();byte[] loginId = mindPackage.getLoginId();byte[] commandCode = mindPackage.getCommandCode();byte[] commandId = mindPackage.getCommandId();byte[] result = mindPackage.getResult();byte[] secret = mindPackage.getSecret();byte[] zip = mindPackage.getZip();byte[] data = mindPackage.getData();byte[] last = mindPackage.getLast();//组建在一起byte[] bytes = ArrayUtil.addAll(head, length, loginId, commandCode, commandId, result, secret, zip, data, last);ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);byteBuffer.order(tioConfig.getByteOrder());byteBuffer.put(bytes);return byteBuffer;}/*** 处理消息包** @param packet* @param channelContext* @throws Exception*/@Overridepublic void handler(Packet packet, ChannelContext channelContext) throws Exception {MindPackage mindPackage = (MindPackage) packet;//报警照片推送if (Remark.PUSH_VIDEO_SUMMARY_OR_ALARM_CODE.equals(HexUtil.encodeHexStr(mindPackage.getCommandCode()))) {BusinessHandle.push_video_summary_or_alarm_code(mindPackage);} else if (Remark.LOGIN_COMMAND_CODE.equals(HexUtil.encodeHexStr(mindPackage.getCommandCode()))) {System.out.println("客户端正在尝试登录:" + mindPackage.getStringData());} else {System.out.println("最终捕获:" + mindPackage.getStringData());Request request = new Request();request.setOrderCode(HexUtil.encodeHexStr(mindPackage.getCommandCode()));request.setRequestTime(new Date());request.setJson(mindPackage.getStringData());IRequestService requestService = SpringUtil.getBean(IRequestService.class);requestService.add(request);}//发消息String account = "{\"UserId\":10145}";mindPackage.setStringData(account);mindPackage.setCommandCode(HexUtil.decodeHex("0000"));mindPackage.setLoginId(HexUtil.decodeHex("A1270000"));Tio.send(channelContext, mindPackage);}
}

服务端监听器实现

package com.demo.socket.socketplus.server;import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioListener;/*** @author Lenovo 监听器*/
public class ServerAioListenerImpl implements ServerAioListener {@Overridepublic boolean onHeartbeatTimeout(ChannelContext channelContext, Long aLong, int i) {return false;}@Overridepublic void onAfterConnected(ChannelContext channelContext, boolean b, boolean b1) throws Exception {}@Overridepublic void onAfterDecoded(ChannelContext channelContext, Packet packet, int i) throws Exception {}@Overridepublic void onAfterReceivedBytes(ChannelContext channelContext, int i) throws Exception {}@Overridepublic void onAfterSent(ChannelContext channelContext, Packet packet, boolean b) throws Exception {}@Overridepublic void onAfterHandled(ChannelContext channelContext, Packet packet, long l) throws Exception {}@Overridepublic void onBeforeClose(ChannelContext channelContext, Throwable throwable, String s, boolean b) throws Exception {}
}

启动

public static ClientChannelContext starter() throws Exception {ClientTioConfig clientTioConfig = new ClientTioConfig(new ClientAioHandlerImpl(), new ClientAioListenerImpl(), new ReconnConf());TioClient tioClient = new TioClient(clientTioConfig);return tioClient.connect(new Node("192.168.0.1", 8000));}
public static TioServer starter() throws IOException {ServerTioConfig serverTioConfig = new ServerTioConfig("tio-server", new ServerAioHandlerImpl(), new ServerAioListenerImpl());tioServer = new TioServer(serverTioConfig);tioServer.start(null, 8080);return tioServer;}

注意:4byte记录了包长度,低位在前。函数如下:

/*** 标识包长度的4byte转成int数字*/public static int bytes2int(byte[] bytes) {Assert.isTrue(bytes.length == 4, "只支持byte[]长度为4的转换");String picLenStr1 = HexUtil.encodeHexStr(bytes);String newPicLenStr1 = picLenStr1.substring(6, 8) + picLenStr1.substring(4, 6)+ picLenStr1.substring(2, 4) + picLenStr1.substring(0, 2);return Integer.parseInt(newPicLenStr1, 16);}

补贴几个类:

package com.demo.socket.util;import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.HexUtil;
import org.springframework.util.StringUtils;import java.io.*;
import java.nio.charset.StandardCharsets;/*** @author xuhj* @description* @date 2021/4/28 15:37*/
public class HexStrUtil {/*** 将十六进制的字符串转换成字节数组** @param hexString* @return*/public static byte[] hexStrToBinaryStr(String hexString) {if (StringUtils.isEmpty(hexString)) {return null;}hexString = hexString.replaceAll(" ", "");int len = hexString.length();int index = 0;byte[] bytes = new byte[len / 2];while (index < len) {String sub = hexString.substring(index, index + 2);bytes[index / 2] = (byte) Integer.parseInt(sub, 16);index += 2;}return bytes;}/*** 将字节数组转换成十六进制的字符串** @return*/public static String binaryToHexString(byte[] bytes, int length) {String hexStr = "0123456789ABCDEF";String result = "";String hex = "";int len = 0;for (byte b : bytes) {len++;hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));hex += String.valueOf(hexStr.charAt(b & 0x0F));result += hex;if (len == length) {break;}}return result;}/*** 16进制字符串转换为字符串** @param s* @return*/public static String hexStringToString(String s) {if (s == null || "".equals(s)) {return null;}s = s.replace(" ", "");byte[] baKeyword = new byte[s.length() / 2];for (int i = 0; i < baKeyword.length; i++) {try {baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));} catch (Exception e) {e.printStackTrace();}}try {s = new String(baKeyword, StandardCharsets.UTF_8);} catch (Exception e1) {e1.printStackTrace();}return s;}/*** 字符串转为16进制字符串*/public static String convertStringToHex(String str) {char[] chars = str.toCharArray();StringBuffer hex = new StringBuffer();for (char aChar : chars) {hex.append(Integer.toHexString((int) aChar));}return hex.toString().toUpperCase();}public static String txt2String(File file) {StringBuilder result = new StringBuilder();try {BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件String s = null;while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行result.append(System.lineSeparator() + s);}br.close();} catch (Exception e) {e.printStackTrace();}return result.toString();}/*** 将16进制转换为二进制** @param hexStr* @return*/public static byte[] parseHexStr2Byte(String hexStr) {if (hexStr.length() < 1)return null;byte[] result = new byte[hexStr.length() / 2];for (int i = 0; i < hexStr.length() / 2; i++) {int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),16);result[i] = (byte) (high * 16 + low);}return result;}public static void byteToFile(byte[] contents, String filePath) {BufferedInputStream bis = null;FileOutputStream fos = null;BufferedOutputStream output = null;try {ByteArrayInputStream byteInputStream = new ByteArrayInputStream(contents);bis = new BufferedInputStream(byteInputStream);File file = new File(filePath);// 获取文件的父路径字符串File path = file.getParentFile();if (!path.exists()) {boolean isCreated = path.mkdirs();if (!isCreated) {}}fos = new FileOutputStream(file);// 实例化OutputString 对象output = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int length = bis.read(buffer);while (length != -1) {output.write(buffer, 0, length);length = bis.read(buffer);}output.flush();} catch (Exception e) {} finally {try {bis.close();fos.close();output.close();} catch (IOException e0) {}}}/*** 标识包长度的4byte转成int数字*/public static int bytes2int(byte[] bytes) {Assert.isTrue(bytes.length == 4, "只支持byte[]长度为4的转换");String picLenStr1 = HexUtil.encodeHexStr(bytes);String newPicLenStr1 = picLenStr1.substring(6, 8) + picLenStr1.substring(4, 6)+ picLenStr1.substring(2, 4) + picLenStr1.substring(0, 2);return Integer.parseInt(newPicLenStr1, 16);}/*** 标识包长度的int数字转成4byte*/public static byte[] int2bytes(int len) {String hexString = Integer.toHexString(len);int length = 8-hexString.length();for (int i = 0; i < length; i++) {hexString = "0" + hexString;}String newPicLenStr1 = hexString.substring(6, 8) + hexString.substring(4, 6)+ hexString.substring(2, 4) + hexString.substring(0, 2);return HexUtil.decodeHex(newPicLenStr1);}/*** 标识包长度的int数字转成16进制字符串*/public static String int2HexString(int len) {String hexString = Integer.toHexString(len);int length = 8-hexString.length();for (int i = 0; i < length; i++) {hexString = "0" + hexString;}return hexString.substring(6, 8) + hexString.substring(4, 6)+ hexString.substring(2, 4) + hexString.substring(0, 2);}/*** 16进制字符串转成int数字*/public static int hexString2Int(String hex) {String s = hex.substring(6, 8) + hex.substring(4, 6)+ hex.substring(2, 4) + hex.substring(0, 2);return Integer.parseInt(s, 16);}public static void main(String[] args) {
//        System.out.println(convertStringToHex("{\"RequstId\":\"0\",\"AlarmId\":\"B16ACC7E-4AB6-4DBF-A245-9A54AEECFEBB\",\"FormatType\":1,\"PushNotice\":false}"));//System.out.println(Integer.toHexString(111));//System.out.println(hexStringToString("7B22557365724964223A31303033357D"));//File file = new File("C:\\Users\\xuhj\\Desktop\\新建文本文档.txt");//byteToFile(parseHexStr2Byte(txt2String(file).trim()), "C:\\Users\\xuhj\\Desktop\\1.mp4");int i = Integer.parseInt("4FD5B651", 16);System.out.println(i);String s = Integer.toHexString(15);System.out.println(s);
//        byte[] bytes = int2bytes(1098674434);
//        int i = bytes2int(bytes);
//        System.out.println(i);}
}
package com.demo.socket.entity;import com.baomidou.mybatisplus.annotation.IdType;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;/*** <p>* * </p>** @author xuhj* @since 2021-05-09*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="Response对象", description="")
public class Response implements Serializable {private static final long serialVersionUID = 1L;@ApiModelProperty(value = "序号")@TableId(value = "id", type = IdType.AUTO)private Integer id;@ApiModelProperty(value = "命令id")private String orderId;@ApiModelProperty(value = "命令码")private String orderCode;@ApiModelProperty(value = "响应json")private String json;@ApiModelProperty(value = "响应时间")private Date responseTime;@ApiModelProperty(value = "状态0前端未获取1已获取")private Integer status;@ApiModelProperty(value = "报警id")private String alarmId;@ApiModelProperty(value = "分段id")private Integer partNo;
}
package com.demo.socket.socketplus;/*** 请求标识16进制字符串(命令ID)*/
public interface Remark {/*** 自定义-登录命令ID*/String LOGIN_COMMAND_ID = "01 00 00 00".replaceAll(" ", "");/*** 登录命令CODE*/String LOGIN_COMMAND_CODE = "01 00".replaceAll(" ", "");/*** 推送视频摘要或报警CODE*/String PUSH_VIDEO_SUMMARY_OR_ALARM_CODE = "05 00".replaceAll(" ", "");/*** 自定义-请求录像传送计划ID*/String REQUEST_VIDEO_TRANSMISSION_PLAN_COMMAND_ID = "01 00 00 01".replaceAll(" ", "");/*** 请求录像传送计划CODE*/String REQUEST_VIDEO_TRANSMISSION_PLAN_COMMAND_CODE = "06 00".replaceAll(" ", "");/*** 请求录像片段CODE*/String REQUEST_VIDEO_PART_COMMAND_CODE = "07 00".replaceAll(" ", "");/*** 请求录像传送计划下载完成CODE*/String REQUEST_VIDEO_TRANSMISSION_PLAN_FINISH_COMMAND_CODE = "27 00".replaceAll(" ", "");/*** 自定义-请求录像片段ID*/String REQUEST_VIDEO_CLIP_COMMAND_ID = "01 00 00 10".replaceAll(" ", "");/*** 请求录像片段CODE*/String REQUEST_VIDEO_CLIP_COMMAND_CODE = "07 00".replaceAll(" ", "");}
package com.demo.socket.service;import com.demo.socket.entity.Response;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.core.metadata.IPage;import java.util.List;/*** <p>*  服务类* </p>** @author xuhj* @since 2021-05-09*/
public interface IResponseService extends IService<Response> {/*** 查询分页数据** @param page      页码* @param pageCount 每页条数* @return IPage<Response>*/IPage<Response> findListByPage(Integer page, Integer pageCount);Response query(String orderId);List<Response> findByAlarmId(String alarmId);List<Response> findByCodeAndAlarmId(String orderCode, String alarmId);/*** 添加** @param response * @return int*/int add(Response response);/*** 删除** @param id 主键* @return int*/int delete(Integer id);/*** 修改** @param response * @return int*/int updateData(Response response);/*** id查询数据** @param id id* @return Response*/Response findById(Integer id);
}

tio对接天眼tcp协议记录相关推荐

  1. 对接物联网设备tcp协议_什么是物联网?常见IoT协议最全讲解

    本文介绍物联网基础知识:什么是物联网,以及常见的物联网协议. 一.什么是物联网? 物联网(Internet of Things)这个概念读者应该不会陌生.物联网的概念最早于1999年被提出来,曾被称为 ...

  2. java socket发送定长报文_一个基于TCP协议的Socket通信实例

    原标题:一个基于TCP协议的Socket通信实例 1. 前言 一般接口对接多以http/https或webservice的方式,socket方式的对接比较少并且会有一些难度.正好前段时间完成了一个so ...

  3. Linux网络-UDP/TCP协议详解

    Linux网络-UDP/TCP协议详解 零.前言 一.UDP协议 二.TCP协议 1.应答机制 2.序号机制 3.超时重传机制 4.连接管理机制 三次握手 四次挥手 5.理解CLOSE_WAIT状态 ...

  4. 传输层协议之TCP协议详解

    传输层重点协议:UDP和TCP. 作用:负责数据能够从发送端传输到接收端.(在进行网络编程时,我们会使用到socket,然而一旦调用socket就进入到了传输层的范畴内) 前面我们已经讲过UDP协议了 ...

  5. 第二篇:可靠连接,TCP协议全解析

    文章目录 一.前言 二.TCP报文结构 三.TCP连接(重点:三次握手和四次挥手) 3.1 三次握手建立连接 3.2 四次挥手释放连接 3.3 wireshark演示 四.TCP可靠连接 4.1 序号 ...

  6. TCP协议中的URG和PSH位

    相关背景知识 http://blog.csdn.net/double_happiness/article/details/74025156 在探讨TCP协议中的URG和PSH控制位时,我们先来简单的复 ...

  7. 面试:TCP协议经典十五连问!

    以下文章来源方志朋的博客,回复"666"获面试宝典 前言 TCP协议是大厂面试必问的知识点.整理了15道非常经典的TCP面试题,希望大家都找到理想的offer呀 1.  讲下TCP ...

  8. 五分钟读懂TCP 协议

    点击上方"视学算法",选择"置顶或者星标" 第一时间阅读精彩文章! 作者:阮一峰 地址:http://www.ruanyifeng.com/blog/2017/ ...

  9. 易语言tcp多线程服务端客户端_从TCP协议到TCP通信的各种异常现象和分析

    很多人总觉得学习TCP/IP协议没什么用,觉得日常编程开发只需要知道socket接口怎么用就可以了.如果大家定位过线上问题就会知道,实际上并非如此.如果应用在局域网内,且设备一切正常的情况下可能确实如 ...

最新文章

  1. 软件构建之链接应用--链接脚本
  2. 【原创】完美实现GetProcAddress [文字模式]
  3. Java5的 线程并发库
  4. 用神经网络分类3*3矩阵
  5. webpack-dev-server启动后,localhost:8080返回index.html的原理
  6. 网管笔记(1)8.22
  7. C4D素材背景模板|广泛应用电商场景和展览的运用
  8. 64位浮点数_C语言常见面试题解析(1)——浮点数的比较
  9. 谷歌将彻底淘汰10年前发布的Android版本
  10. java制作纯字rpg小游戏_求java rpg小游戏源代码 最好是文字rpg 不需要很复杂 只是交作业用...
  11. WIN7专业版下安装MS SQL SERVER 2005 开发版
  12. Word参考文献设置-尾注后添加致谢等内容
  13. 高通Thermal Overview之thermal-engine
  14. ​在线问题反馈模块实战(十二)​:实现图片删除功能
  15. N卡和A卡各型号参数对比收集
  16. 宇宙文明等级的划分标准
  17. Python 爬虫实战1.0
  18. unity材质球发光_Unity利用材质自发光实现物体闪烁
  19. arcgis几何修复有作用吗_ArcGIS修复几何错误
  20. IEEE文献基础检索技巧

热门文章

  1. log4j漏洞分析及总结
  2. OpenGL实现图片边缘暗角效果(1)
  3. 请求一次服务器会显示请求了两次,在Appengin中,每个请求都会到达服务器两次...
  4. char matlab中,matlab中char什么意思
  5. 两个实打实干活的同事离职了,老板连谈都没谈,一句挽留都没有,你怎么看?
  6. 程序员搞开源,读什么书最合适?
  7. Android语音播报、后台播报、语音识别
  8. Android studio使用中问题集锦
  9. Spring Boot入门教程(四十六): @Async
  10. 记一次Very Animation动画插件使用