我正在尝试为Teltonika(“ GPS跟踪设备”)创建一个简单的服务器,该服务器除了保存位置外没有其他用途。
我到达了Teltonika向服务器发送带有标识(IMEI)的消息的地步,但随后我必须做出以下回应:
服务器收到IMEI后,应确定是否接受数据 从这个模块。如果是,则服务器将回复模块01(如果不是00)。 请注意,确认应作为二进制数据包发送。然后模块 开始发送第一个AVL数据包。服务器收到数据包后, 解析它,服务器必须报告接收到的模块数据的数量 整数(四个字节)。如果发送数据号并由服务器报告 与模块不匹配会重新发送已发送的数据。
所以我必须以“二进制数据包”的形式发送01,但是我不知道如何在netty中(通过TCP)发送二进制数据包,有人可以告诉我该怎么做吗?
我已经尝试过的方法:
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf inBuffer = (ByteBuf) msg;
String received = inBuffer.toString(CharsetUtil.UTF_8);
System.out.println(dateFormat.format(new Date()) + " Server received: " + received);
byte[] out = new byte[] { 0, 1 };
ByteBuf encoded = ctx.alloc().buffer(4);
encoded.writeBytes(out);
ctx.writeAndFlush(encoded);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
我现在已经尝试过 out =新字节[] {0x01}
并向每个方法添加sysout(channelread和channelreadcomplete除外) 输出如下:
channelRegistered
channelActive
19:41:08 Server received: 352094088680829
channelInactive
channelUnregistered
channelRegistered
channelActive
19:41:19 Server received: 352094088680829
channelInactive
channelUnregistered
channelRegistered
channelActive
19:41:30 Server received: 352094088680829
...
我现在正处在Teltonika发送第二个数据包的位置(我认为其中包含位置数据,但必须进一步检查)
我使用的代码
服务器:
import java.net.InetSocketAddress;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class MainNettyApplicationServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(group);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.localAddress(new InetSocketAddress("10.0.0.2", 11111));
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new SimpleServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind().sync();
System.out.println("Server started.");
channelFuture.channel().closeFuture().sync();
} catch(Exception e){
e.printStackTrace();
} finally {
group.shutdownGracefully().sync();
}
}
}
处理程序:
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class SimpleServerHandler extends ChannelInboundHandlerAdapter {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf inBuffer = (ByteBuf) msg;
String received = inBuffer.toString(CharsetUtil.UTF_8);
System.out.println(dateFormat.format(new Date()) + " Server received: " + received);
byte[] out = new byte[] { 0x01 };
ByteBuf encoded = ctx.alloc().buffer(1);
encoded.writeBytes(out);
ctx.writeAndFlush(encoded);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
}