NettyUDPConnector.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:mpush 作者:
private void createServer(Listener listener, EventLoopGroup eventLoopGroup, ChannelFactory<? extends DatagramChannel> channelFactory) {
    this.eventLoopGroup = eventLoopGroup;
    try {
        Bootstrap b = new Bootstrap();
        b.group(eventLoopGroup)//默认是根据机器情况创建Channel,如果机器支持ipv6,则无法使用ipv4的地址加入组播
                .channelFactory(channelFactory)
                .option(ChannelOption.SO_BROADCAST, true)
                .handler(getChannelHandler());

        initOptions(b);

        //直接绑定端口,不要指定host,不然收不到组播消息
        b.bind(port).addListener(future -> {
            if (future.isSuccess()) {
                logger.info("udp server start success on:{}", port);
                if (listener != null) listener.onSuccess(port);
            } else {
                logger.error("udp server start failure on:{}", port, future.cause());
                if (listener != null) listener.onFailure(future.cause());
            }
        });
    } catch (Exception e) {
        logger.error("udp server start exception", e);
        if (listener != null) listener.onFailure(e);
        throw new ServiceException("udp server start exception, port=" + port, e);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号