1
votes

I have been trying to learn Netty and set up a simple server client connection following a tutorial but whenever I run this part of the code:

    public void start() throws Exception{

    EventLoopGroup group = new NioEventLoopGroup();

    try {
        Bootstrap b = new Bootstrap();
        b.group(group)
            .channel(NioSocketChannel.class)
            .remoteAddress(new InetSocketAddress(host,port))
            .handler(new ChannelInitializer<SocketChannel>() {

                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new EchoClientHandler());
                }
            });
        ChannelFuture f = b.bind().sync();
        System.out.println("ChannelFuture bind is a success");
        f.channel().closeFuture().sync();
        System.out.println("ChannelFuture has been closed");
    } finally {
        group.shutdownGracefully().sync();
    }
}

This is part of the client program which is trying to connect to 127.0.0.1 on port 1234. I run this after I set up the server program which is running on the same computer on port 1234.

The error I get is:

Exception in thread "main" java.lang.IllegalStateException: localAddress not set at io.netty.bootstrap.AbstractBootstrap.bind(AbstractBootstrap.java:235) at EchoClient.EchoClient.start(EchoClient.java:39) at EchoClient.EchoClient.main(EchoClient.java:57)

The line java:39 is referencing is "ChannelFuture f = b.bind().sync();" I don't quite understand what localAddress is and why I would need a local address if it is the client program.

I'm running this program on a Linux Virtual Machine if that makes a difference.

1

1 Answers

3
votes

I accidentally wrote b.bind().sync(); instead of b.connect().sync(); in the client program