main
黄海 2 years ago
parent a023089b9d
commit 8e3b8d57f5

@ -1,23 +1,18 @@
package UnitTest; package UnitTest;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.Socket; import java.net.Socket;
public class HexStringSender { public class HexStringSender {
public static String IP = "10.10.21.18"; public static String IP = "10.10.21.18";
public static int PORT = 8001; public static int PORT = 8001;
//# 开 //# 开
public static String open_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 01"; public static String open_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 01";
//关 //关
public static String close_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 00"; public static String close_hex_str = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 00";
public static void main(String[] args) throws IOException, InterruptedException { public static void main(String[] args) throws IOException, InterruptedException {
for (int i = 1; ; i++) { for (int i = 1; ; i++) {
Socket socket = new Socket(IP, PORT); Socket socket = new Socket(IP, PORT);
String hex_str; String hex_str;
@ -30,7 +25,6 @@ public class HexStringSender {
Thread.sleep(2000); Thread.sleep(2000);
} }
} }
/** /**

@ -0,0 +1,30 @@
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 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);
}
}
}

@ -1,4 +1,4 @@
package UnitTest; package com.dsideal.FengHuang.Util;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
@ -9,11 +9,11 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
public class HexDataSender { public class SocketUtil {
private final String host; private final String host;
private final int port; private final int port;
public HexDataSender(String host, int port) { public SocketUtil(String host, int port) {
this.host = host; this.host = host;
this.port = port; this.port = port;
} }
@ -29,44 +29,23 @@ public class HexDataSender {
b.handler(new ChannelInitializer<SocketChannel>() { b.handler(new ChannelInitializer<SocketChannel>() {
@Override @Override
public void initChannel(SocketChannel ch) { public void initChannel(SocketChannel ch) {
//ChannelPipeline p = ch.pipeline(); ChannelPipeline p = ch.pipeline();
//p.addLast(new HexDataEncoder()); p.addLast(new HexDataEncoder());
//p.addLast(new HexDataSenderHandler(hexData)); p.addLast(new HexDataSenderHandler(hexData));
} }
}); });
// Start the client. // Start the client.
ChannelFuture f = b.connect(host, port).sync(); ChannelFuture f = b.connect(host, port).sync();
f.channel().writeAndFlush(Unpooled.copiedBuffer(hexData.getBytes())).sync();
// Wait until the connection is closed. // Wait until the connection is closed.
f.channel().closeFuture().sync(); //f.channel().closeFuture().sync();
//手动关闭
f.channel().close();
} finally { } finally {
workerGroup.shutdownGracefully(); workerGroup.shutdownGracefully();
} }
} }
public static String IP = "10.10.21.18";
public static int PORT = 8001;
public static void Open() throws Exception {
//开
String hexData = "16 00 34 F5 41 11 FE 82 0D 02 6D FF 00 00 00 00 00 00 01 00 00 01".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");
Open();
}
} }
class HexDataSenderHandler extends ChannelInboundHandlerAdapter { class HexDataSenderHandler extends ChannelInboundHandlerAdapter {
Loading…
Cancel
Save