我使用Netty 4与以HEX方式发送数据的GPS设备进行通信。 问题是在Netty我接收到一些奇怪的消息($$ .. 0 ..... @ .. y ..)而不是HEX数据(00 00 19 40 00 02 79 1d 0d 0a)我应该读。 我用来处理消息的方法非常简单:
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
String message = in.toString(io.netty.util.CharsetUtil.US_ASCII);
}
这是我的主要方法:
public static void main(String args[]) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new GpsMessageHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).await();
f.channel().closeFuture().syncUninterruptibly();
}
catch (Exception e) {
}
finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
任何帮助将不胜感激。