Search code examples
javaudpnetty

Netty connect() throwing io.netty.channel.AbstractChannel$AnnotatedSocketException: Invalid argument exception


This is the portion of the sample code I wrote

b = new Bootstrap();
        var remote = new InetSocketAddress("www.google.com", 8080);
        b.group(group)
                .channel(NioDatagramChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        channel.pipeline().addLast(new MyHandler());
                    }
                });
        var cf = b.connect(remote, new InetSocketAddress(8536)).sync(); // this line throws Exception

This is the Exception printed in the terminal

io.netty.channel.AbstractChannel$AnnotatedSocketException: Invalid argument: www.google.com/172.217.194.105:48830

Is there a reason for this error? What's the fix?

Full stacktrace

io.netty.channel.AbstractChannel$AnnotatedSocketException: Invalid argument: www.google.com/172.217.194.105:48830
Caused by: java.net.SocketException: Invalid argument
        at java.base/sun.nio.ch.Net.connect0(Native Method)
        at java.base/sun.nio.ch.Net.connect(Net.java:476)
        at java.base/sun.nio.ch.DatagramChannelImpl.connect(DatagramChannelImpl.java:848)
        at io.netty.channel.socket.nio.NioDatagramChannel.doConnect(NioDatagramChannel.java:215)
        at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:248)
        at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1342)
        at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548)
        at io.netty.channel.AbstractChannelHandlerContext.connect(AbstractChannelHandlerContext.java:533)
        at io.netty.channel.DefaultChannelPipeline.connect(DefaultChannelPipeline.java:984)
        at io.netty.channel.AbstractChannel.connect(AbstractChannel.java:258)
        at io.netty.bootstrap.Bootstrap$3.run(Bootstrap.java:252)
        at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
        at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
        at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
        at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
        at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
        at java.base/java.lang.Thread.run(Thread.java:834)

Solution

  • Use:

     b.connect(remote, new InetSocketAddress("127.0.0.1",8536)).sync()