简体   繁体   English

Netty中的java.net.SocketException

[英]java.net.SocketException in Netty

When I bind "127.0.0.1", it works without exception, but when I bind "172.16.3.138", after it runs for 15 seconds, an exception is thrown: 当我绑定“ 127.0.0.1”时,它毫无例外地起作用,但是当我绑定“ 172.16.3.138”时,它运行15秒后,会引发异常:

java.net.SocketException: Can't assign requested address:/172.16.3.138:8888 java.net.SocketException:无法分配请求的地址:/172.16.3.138:8888

I'm using Netty-4.033 on Mac. 我在Mac上使用Netty-4.033。

My Server Code: 我的服务器代码:

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();
    }

My client Code: 我的客户代码:

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);
            }
        }
    });
}

Are you sure the address you provide (172.16.3.138) is the address of the computer the program is running on? 您确定提供的地址(172.16.3.138)是程序正在运行的计算机的地址吗?

Binding to an IP other than loopback and localhost is designed to bind to another network adapter (on the same computer). 绑定到除回送和本地主机之外的IP旨在绑定到另一个网络适配器(在同一台计算机上)。

When you are calling to 127.0.0.1:8888 you are connecting in a direct way with you own machine, and the port 8888 in that case is open. 当您呼叫127.0.0.1:8888您将直接与自己的计算机连接,在这种情况下,端口8888是开放的。 But when you call to 172.16.3.138:8888 you are connecting with you own machine too, but you need pass through a router or another server proxy which could have the port 8888 closed. 但是,当您致电172.16.3.138:8888您也正在与自己的计算机连接,但是您需要通过路由器或其他服务器代理(可能关闭了端口8888)。 Please verify this and try again. 请验证,然后重试。 (maybe you need to open the port 8888 in you router). (也许您需要在路由器中打开端口8888)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM