zoukankan      html  css  js  c++  java
  • netty

    import com.yd.lbs.gps.acceptor.util.PropertiesUtil;
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.buffer.PooledByteBufAllocator;
    import io.netty.channel.*;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    
    public class Jt808GpsServer {
    
        private static Logger logger = LoggerFactory.getLogger(Jt808GpsServer.class);
    
        public static void main(String[] args) throws InterruptedException {
            int port = PropertiesUtil.GPS_PORT;
            if (args != null && args.length > 0) {
                try {
                    port = Integer.parseInt(args[0]);
                } catch (NumberFormatException e) {
                }
            }
            logger.info("Jt808 gps server port is:{} ", port);
            new Jt808GpsServer().bind(port);
        }
    
        public void bind(int port) throws InterruptedException {
    
            //EventLoopGroup 类似线程组,boss用于监听是否有tcp链接过来;worker用于建立链接并处理
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            //如果不设置参数值,则默认取CPU核心数*2
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 100)
                        .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                        .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                        .handler(new LoggingHandler(LogLevel.INFO))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) {
                                ChannelPipeline pipeline = ch.pipeline();
                                pipeline.addLast("decode", new Jt808MessageDecoder());
                                pipeline.addLast("encode", new Jt808MessageEncoder());
                                pipeline.addLast(new Jt808GpsServerHandler());
    
                            }
                        });
                //绑定端口,同步等待成功
                ChannelFuture f = b.bind(port).sync();
                //等待服务器端监听端口关闭
                f.channel().closeFuture().sync();
                logger.info("Jt808 gps server is started up...");
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    }
  • 相关阅读:
    Codeforces Ilya and Matrix
    poj 1308 Is It A Tree?
    Codeforces Sereja and Array
    poj 1041 John's trip
    Codeforces Continued Fractions
    WM_COPYDATA实现进程间数据通信
    虚拟机中安装ubuntu后,终端模式和图形模式切换
    IIS上配置运行cgi,php,aspx运行环境
    linux c main函数参数
    Linux网络编程入门 (转载)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6541953.html
Copyright © 2011-2022 走看看