main
黄海 2 years ago
parent 1dfb085d19
commit 5e0561f866

@ -285,6 +285,12 @@
<artifactId>kafka-clients</artifactId>
<version>3.4.0</version>
</dependency>
<!--引入netty-->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
</dependencies>
<build>
<plugins>

@ -0,0 +1,111 @@
package UnitTest;
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;
public class HexDataSender {
private final String host;
private final int port;
public HexDataSender(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));
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
public static String IP = "10.10.21.18";
public static int PORT = 8001;
public static void Open(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(" ", "");
HexDataSender sender = new HexDataSender(IP, PORT);
sender.sendHexData(hexData);
}
public static void Close(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(" ", "");
HexDataSender sender = new HexDataSender(IP, PORT);
sender.sendHexData(hexData);
}
public static void main(String[] args) throws Exception {
Close("6D FF");
//Thread.sleep(3000);
//Open("6D FF");
}
}
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;
}
}
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);
}
}

@ -1,6 +1,6 @@
{
"title": "党建知识问答排名结果",
"sheetName": "结果",
"title": "长春市教育局学习贯彻习近平新时代中国特色社会主义思想知识答题",
"sheetName": "结果",
"titleHeight": 30,
"rowHeight": 30,
"showNumber": true,
@ -8,37 +8,37 @@
{
"show_column_name": "姓名",
"list_column_name": "person_name",
"width": 40
"width": 20
},
{
"show_column_name": "处室",
"list_column_name": "ks",
"width": 40
"width": 30
},
{
"show_column_name": "电话",
"list_column_name": "tel",
"width": 40
"width": 30
},
{
"show_column_name": "得分",
"list_column_name": "score",
"width": 40
"width": 20
},
{
"show_column_name": "用时",
"list_column_name": "ys",
"width": 40
"width": 20
},
{
"show_column_name": "开始时间",
"list_column_name": "start_time",
"width": 40
"width": 32
},
{
"show_column_name": "交卷时间",
"list_column_name": "end_time",
"width": 40
"width": 32
}
]
}
Loading…
Cancel
Save