【问题标题】:On Netty, how to use same IP/PORT of ServerBootstrap on a client (Boostrap) connection?在 Netty 上,如何在客户端(Boostrap)连接上使用 ServerBootstrap 的相同 IP/PORT?
【发布时间】:2021-03-08 23:34:06
【问题描述】:

我有一个侦听 TCP 端口 5000 的 VoIP 服务器。此端口对于所有接收和建立的连接都是必需的。 由于服务器引导程序使用端口 5000,如何创建源端口 = 5000 的客户端 TCP 连接?

示例场景: 客户 A 呼叫客户 B。需要进行两个单独的呼叫:

  1. A(端口 5111)-> 服务器(端口 5000)
  2. 服务器(端口 5000)-> B(端口 9999)

在调用 1 时,目标端口 = 5000 (ServerBootstrap)。 On call 2 源端口需要为 5000(客户端引导)。

谁能给我一盏灯?

服务器

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

客户端代码

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

【问题讨论】:

    标签: java sockets netty


    【解决方案1】:

    您可以使用 Bootstrap 类的以下方法添加套接字地址:

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

    在您的示例中,可以如下完成:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多