Netty - Initiating new connection with the same li

2019-08-20 02:37发布

问题:

I have a bound server channel that is currently accepting connections at local address 'x'. I now need to initiate a connection to a remote address 'y', but I need my local listening address to be 'x' as well. When working with the local channel factory and local addresses, I get a 'address already bound' error when I try to make a new client connection with local address = 'x'. This makes sense.

So my next route was trying to find a way to create a new child connection from the server channel, but this ended up going a little too deep into the netty internal code and seemed like a bad route to go. I remember seeing a bug earlier about local client connections.

  • Is this just a bug with local channels?
  • Is initiating a connection from a bound address possible in Netty?

Thanks, Daniel

回答1:

Are you writing a proxy? If so, here's a netty example app.

Note that you have to setup different channel factories for client and server.

    // Configure the bootstrap.
    Executor executor = Executors.newCachedThreadPool();
    ServerBootstrap sb = new ServerBootstrap(
            new NioServerSocketChannelFactory(executor));

    // Set up the event pipeline factory.
    ClientSocketChannelFactory cf =
            new NioClientSocketChannelFactory(executor);


标签: jboss netty