main
黄海 2 years ago
parent 8e3b8d57f5
commit 9e03fb2c59

@ -17,6 +17,15 @@ public class SocketUtilTest {
SocketUtil sender = new SocketUtil(host, port); SocketUtil sender = new SocketUtil(host, port);
sender.sendHexData(hexData); 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 { public static void main(String[] args) throws Exception {
String HOST = "10.10.21.18"; String HOST = "10.10.21.18";
int PORT = 8001; int PORT = 8001;
@ -26,5 +35,6 @@ public class SocketUtilTest {
else Close(HOST, PORT, sb_id); else Close(HOST, PORT, sb_id);
Thread.sleep(1000); Thread.sleep(1000);
} }
// getList(HOST,PORT);
} }
} }

@ -9,6 +9,8 @@ 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;
import java.nio.charset.StandardCharsets;
public class SocketUtil { public class SocketUtil {
private final String host; private final String host;
private final int port; private final int port;
@ -32,6 +34,7 @@ public class SocketUtil {
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));
p.addLast(new ClientHandler()); // 添加客户端 Handler
} }
}); });
@ -46,45 +49,64 @@ public class SocketUtil {
} }
} }
} class HexDataSenderHandler extends ChannelInboundHandlerAdapter {
private final String hexData;
class HexDataSenderHandler extends ChannelInboundHandlerAdapter { public HexDataSenderHandler(String hexData) {
private final String hexData; this.hexData = hexData;
}
public HexDataSenderHandler(String hexData) { @Override
this.hexData = hexData; 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;
}
@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) { static class ClientHandler extends ChannelInboundHandlerAdapter {
int len = hexData.length();
byte[] data = new byte[len / 2]; @Override
for (int i = 0; i < len; i += 2) { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
data[i / 2] = (byte) ((Character.digit(hexData.charAt(i), 16) << 4) + // 处理服务器响应数据
Character.digit(hexData.charAt(i + 1), 16)); 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();
} }
return data;
} }
}
class HexDataEncoder extends MessageToByteEncoder<ByteBuf> { static class HexDataEncoder extends MessageToByteEncoder<ByteBuf> {
@Override @Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
byte[] bytes = new byte[msg.readableBytes()]; byte[] bytes = new byte[msg.readableBytes()];
msg.getBytes(msg.readerIndex(), bytes); msg.getBytes(msg.readerIndex(), bytes);
out.writeBytes(bytes); out.writeBytes(bytes);
}
} }
} }

Loading…
Cancel
Save