Merge branch 'main' of http://10.10.14.176:3000/huanghai/ccDangJianExam
commit
54146b86e5
@ -0,0 +1,40 @@
|
||||
package UnitTest;
|
||||
|
||||
import com.dsideal.FengHuang.Util.SocketUtil;
|
||||
|
||||
public class SocketUtilTest {
|
||||
|
||||
public static void Open(String host, int port, String sb_id) throws Exception {
|
||||
//开
|
||||
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 01".replace("?", sb_id).replace(" ", "");
|
||||
SocketUtil sender = new SocketUtil(host, port);
|
||||
sender.sendHexData(hexData);
|
||||
}
|
||||
|
||||
public static void Close(String host, int port, String sb_id) throws Exception {
|
||||
//关
|
||||
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 ? 00 00 00 00 00 00 01 00 00 00".replace("?", sb_id).replace(" ", "");
|
||||
SocketUtil sender = new SocketUtil(host, port);
|
||||
sender.sendHexData(hexData);
|
||||
}
|
||||
|
||||
public static void getList(String host, int port) throws Exception {
|
||||
//String gateWay = "1141f534";
|
||||
String hexData = "08 00 34 f5 41 11 FE 81";
|
||||
hexData=hexData.replace(" ","");
|
||||
SocketUtil sender = new SocketUtil(host, port);
|
||||
sender.sendHexData(hexData);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String HOST = "10.10.21.18";
|
||||
int PORT = 8001;
|
||||
String sb_id = "6D FF";
|
||||
for (int i = 1; ; i++) {
|
||||
if (i % 2 == 1) Open(HOST, PORT, sb_id);
|
||||
else Close(HOST, PORT, sb_id);
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
// getList(HOST,PORT);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
Navicat Premium Data Transfer
|
||||
|
||||
Source Server : 10.10.14.169
|
||||
Source Server Type : MySQL
|
||||
Source Server Version : 100505 (10.5.5-MariaDB-log)
|
||||
Source Host : 10.10.14.169:22066
|
||||
Source Schema : ccdjzswd_db
|
||||
|
||||
Target Server Type : MySQL
|
||||
Target Server Version : 100505 (10.5.5-MariaDB-log)
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 10/05/2023 15:21:01
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_sync_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_sync_log`;
|
||||
CREATE TABLE `t_sync_log` (
|
||||
`offset` bigint NOT NULL,
|
||||
`timestamp` bigint NOT NULL,
|
||||
`database` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||
`table` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
|
||||
PRIMARY KEY (`offset`) USING BTREE,
|
||||
INDEX `database`(`database` ASC, `table` ASC, `type` ASC) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
@ -0,0 +1,112 @@
|
||||
package com.dsideal.FengHuang.Util;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class SocketUtil {
|
||||
private final String host;
|
||||
private final int port;
|
||||
|
||||
public SocketUtil(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void sendHexData(String hexData) throws Exception {
|
||||
EventLoopGroup workerGroup = new NioEventLoopGroup();
|
||||
|
||||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
b.group(workerGroup);
|
||||
b.channel(NioSocketChannel.class);
|
||||
b.option(ChannelOption.SO_KEEPALIVE, true);
|
||||
b.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) {
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new HexDataEncoder());
|
||||
p.addLast(new HexDataSenderHandler(hexData));
|
||||
p.addLast(new ClientHandler()); // 添加客户端 Handler
|
||||
}
|
||||
});
|
||||
|
||||
// Start the client.
|
||||
ChannelFuture f = b.connect(host, port).sync();
|
||||
// Wait until the connection is closed.
|
||||
//f.channel().closeFuture().sync();
|
||||
//手动关闭
|
||||
f.channel().close();
|
||||
} finally {
|
||||
workerGroup.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
|
||||
class HexDataSenderHandler extends ChannelInboundHandlerAdapter {
|
||||
private final String hexData;
|
||||
|
||||
public HexDataSenderHandler(String hexData) {
|
||||
this.hexData = hexData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) {
|
||||
byte[] bytes = hexStringToByteArray(hexData);
|
||||
ByteBuf buffer = Unpooled.buffer(bytes.length);
|
||||
buffer.writeBytes(bytes);
|
||||
ctx.writeAndFlush(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
private byte[] hexStringToByteArray(String hexData) {
|
||||
int len = hexData.length();
|
||||
byte[] data = new byte[len / 2];
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
data[i / 2] = (byte) ((Character.digit(hexData.charAt(i), 16) << 4) +
|
||||
Character.digit(hexData.charAt(i + 1), 16));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static class ClientHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
// 处理服务器响应数据
|
||||
byte[] bytes = (byte[]) msg;
|
||||
String response = new String(bytes, StandardCharsets.US_ASCII);
|
||||
System.out.println("Server response: " + response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
|
||||
static class HexDataEncoder extends MessageToByteEncoder<ByteBuf> {
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
|
||||
byte[] bytes = new byte[msg.readableBytes()];
|
||||
msg.getBytes(msg.readerIndex(), bytes);
|
||||
out.writeBytes(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue