Netty中的java.net.SocketException

时间:2016-01-13 09:16:24

标签: java netty nio

当我绑定" 127.0.0.1"时,它无例外地工作,但当我绑定" 172.16.3.138"时,它运行15秒后,抛出异常:

  

java.net.SocketException:无法分配请求的地址:/172.16.3.138:8888

我在Mac上使用Netty-4.033。

我的服务器代码:

EventLoopGroup masterGroup = null;
    EventLoopGroup workGroup = null;
    try {
        masterGroup = new NioEventLoopGroup();
        workGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(masterGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(childHandler)
                .option(ChannelOption.SO_BACKLOG, 100)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress("0.0.0.0",port)).sync();
        channelFuture.channel().closeFuture().sync();
    } catch (Exception e) {
        BaseLog.logger.error("netty server start failed",e);
    } finally {
        if (workGroup != null)
            workGroup.shutdownGracefully();
        if (masterGroup != null)
            masterGroup.shutdownGracefully();
    }

我的客户代码:

public void run() {
    EventLoopGroup group = null;
    int initTimes = 0;
    while (true) {
        try {
            if (initTimes > 50) {
                BaseLog.logger.error("try 50 times to create NioEvent,failed");
                break;
            }
            group = new NioEventLoopGroup();
            break;
        } catch (Exception e) {
            initTimes++;
            sleep(300);
            continue;
        }
    }

    try {
        Bootstrap bootstrap = new Bootstrap().group(group)
                .channel(NioSocketChannel.class)
                .handler(handler)
                .option(ChannelOption.SO_KEEPALIVE, true);

        int tryTimes = 1;
        channel = null;
        connect(bootstrap,tryTimes);
    } catch (Exception e) {
        e.printStackTrace();
        BaseLog.logger.error("try 5 times,netty client connect failed",e);
    } finally {
        if (group != null)
            group.shutdownGracefully();
    }
}
protected void connect(final Bootstrap bootstrap, final int tryTimes) throws InterruptedException, NettyConnectionException {
    if (tryTimes > 5)
        throw new NettyConnectionException("try 5 times failed");
    ChannelFuture channelFuture = null;
    if (!StringUtil.isEmpty(host) && !host.equals("127.0.0.1") && !host.equals("localhost")) {
        channelFuture = bootstrap.connect(new InetSocketAddress(host, port)).sync();
    }
    else
        channelFuture = bootstrap.connect(host, port).sync();
    if (channelFuture == null) return;
    channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                channel = future.channel();
                sendDatas(objs);
            } else {
                connect(bootstrap, tryTimes+1);
            }
        }
    });
}

2 个答案:

答案 0 :(得分:0)

您确定您提供的地址(172.16.3.138)是运行该程序的计算机的地址吗?

绑定到环回以外的IP和localhost旨在绑定到另一个网络适配器(在同一台计算机上)。

答案 1 :(得分:0)

当您致电127.0.0.1:8888时,您正在使用自己的机器直接连接,并且该情况下的端口8888已打开。但是当你打电话给172.16.3.138:8888时,你也正在连接你自己的机器,但是你需要通过路由器或其他服务器代理来关闭端口8888。请验证这一点,然后重试。 (也许你需要在路由器中打开端口8888。)