我有一个voip服务器,可以监听tcp端口5000。此端口对于所有接收和建立的连接都是必需的。既然服务器引导使用的是端口5000,如何创建源端口为5000的客户端tcp连接?
示例场景:客户a呼叫客户b。需要分别拨打两个电话:
a(端口5111)->服务器(端口5000)
服务器(端口5000)->b(端口9999)
呼叫1时,目标端口=5000(服务器引导)。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());
}
});
1条答案
按热度按时间djmepvbi1#
您可以使用以下引导类方法添加套接字地址:
在您的示例中,可以如下所示: