简体   繁体   中英

On Netty, how to use same IP/PORT of ServerBootstrap on a client (Boostrap) connection?

I have a VoIP server that listen on TCP port 5000. This port is mandatory for all received and made connections. How can I create a client TCP connection, with source port = 5000, since the Server bootstrap is using port 5000?

Sample scenario: Customer A calls customer B. Two separated calls need to be made:

  1. A(Port 5111) -> Server(Port 5000)
  2. Server(Port 5000) -> B(Port 9999)

On call 1 the destination port = 5000 (ServerBootstrap). On call 2 Source port needs to be 5000 (Client Bootstrap).

Could someone give me a light?

Server

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap tcpBootstrap = new ServerBootstrap();
        tcpBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            public void initChannel(final SocketChannel ch) throws Exception
            {
                final ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder", new SipMessageStreamDecoder());
                pipeline.addLast("encoder", new SipMessageEncoder());
                pipeline.addLast("handler", new DialerHandlerTCP());
            }
        })
        .option(ChannelOption.SO_BACKLOG, 128)
        .option(ChannelOption.SO_REUSEADDR, true)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
        .childOption(ChannelOption.SO_KEEPALIVE, true)
        .childOption(ChannelOption.TCP_NODELAY, true);
        
        tcpBootstrap.bind(socketAddress).sync().channel();

Client code

    Bootstrap bootstrapClient = new Bootstrap();

        bootstrapClient.group(networkBind.channel.eventLoop()).channel(NioSocketChannel.class)
        .handler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            public void initChannel(final SocketChannel ch) throws Exception
            {
                final ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder", new SipMessageStreamDecoder());
                pipeline.addLast("encoder", new SipMessageEncoder());
                pipeline.addLast("handler", new DialerHandlerTCP());
            }
        });

You can add the socket address with below methods of Bootstrap class:

public Bootstrap remoteAddress(SocketAddress remoteAddress)
public Bootstrap remoteAddress(String inetHost, int inetPort)
public Bootstrap remoteAddress(InetAddress inetHost,int inetPort)

In your example, it can be done like below:

bootstrapClient.group(networkBind.channel.eventLoop())
               .channel(NioSocketChannel.class)
               .remoteAddress(new InetSocketAddress(host, port))
               .handler(new ChannelInitializer<SocketChannel>()
        {
            @Override
            public void initChannel(final SocketChannel ch) throws Exception
            {
                final ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder", new SipMessageStreamDecoder());
                pipeline.addLast("encoder", new SipMessageEncoder());
                pipeline.addLast("handler", new DialerHandlerTCP());
            }
        });
           

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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