Android使用netty接收处理类似808数据

2024-05-06  本文已影响0人  Android戴勤学

1.导入netty.jar包;

2.新建tcpk服务器端

public class TsTCPServer {
    private static Logger log = new Logger();
    private volatile static boolean isRunning = false;

    private static EventLoopGroup bossGroup = null;
    private static EventLoopGroup workerGroup = null;
    //端口号
    private static int port = 7777;

    public TsTCPServer() {
    }

    private static void bind() throws Exception {
        bossGroup = new NioEventLoopGroup();
        workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)//
                .channel(NioServerSocketChannel.class) //
                .childHandler(new ChannelInitializer<SocketChannel>() { //
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast("idleStateHandler",
                                new IdleStateHandler(TPMSConsts.tcp_client_idle_minutes, 0, 0, TimeUnit.MINUTES));
                        ch.pipeline().addLast(new TsDecoder4LoggingOnly());
                        // 1024表示单条消息的最大长度,解码器在查找分隔符的时候,达到该长度还没找到的话会抛异常
                        ch.pipeline().addLast(
                                new DelimiterBasedFrameDecoder(1024, Unpooled.copiedBuffer(new byte[]{0x7e}),
                                        Unpooled.copiedBuffer(new byte[]{0x7e, 0x7e})));
                        ch.pipeline().addLast(new TsServerHandler());
                    }
                }).option(ChannelOption.SO_BACKLOG, 128) //
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        log.info("国标808TCP服务启动完毕,port={}", port);
        ChannelFuture channelFuture = serverBootstrap.bind(port).sync();

        channelFuture.channel().closeFuture().sync();
    }

    public synchronized static void startServer() {
        if (isRunning) {
//            throw new IllegalStateException(getName() + " is already started .");
        return;
        }
        isRunning = true;

        new Thread(() -> {
            try {
                bind();
            } catch (Exception e) {
                log.info("TCP服务启动出错:{}", e.getMessage());
                e.printStackTrace();
            }
        }, getName()).start();
    }

    public synchronized static void stopServer() {
        if (!isRunning) {
            throw new IllegalStateException(getName() + " is not yet started .");
        }
        isRunning = false;

        try {
            Future<?> future = workerGroup.shutdownGracefully().await();
            if (!future.isSuccess()) {
                log.error("workerGroup 无法正常停止:{}", future.cause());
            }

            future = bossGroup.shutdownGracefully().await();
            if (!future.isSuccess()) {
                log.error("bossGroup 无法正常停止:{}", future.cause());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.info("TCP服务已经停止...");
    }

    private static String getName() {
        return "Jt808TCPServer";
    }

}
上一篇下一篇

猜你喜欢

热点阅读