代码之家  ›  专栏  ›  技术社区  ›  Gorgeo

Netty framework-多端口侦听

  •  0
  • Gorgeo  · 技术社区  · 6 年前

    我有以下问题。我为自己修改了这个示例: Webcam Capture Live Streaming Example

    此时,通信看起来像是client1将映像发送到服务器,而来自服务器的映像则发送到client2。如果我只使用一个摄像头,就没有问题。如果我从两个不同的摄像头播放流媒体,就会出现问题。我希望client1将映像发送到特定端口上的服务器,并且只有在该端口上,服务器才会将映像发送到客户端。2目前(我不知道为什么)服务器得到的是什么,例如,在端口2000上,它发送到所有端口,而不仅仅是端口2000。你能帮帮我吗?

    来自服务器的一些代码:

    @Override
    public void start(SocketAddress streamAddress) {
        logger.info("server started:{}", streamAddress);
        Channel channel = serverBootstrap.bind(streamAddress);
        channelGroup.add(channel);
    }
    

        this.serverBootstrap = new ServerBootstrap();
        this.serverBootstrap.setFactory(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        this.serverBootstrap.setPipelineFactory(new StreamServerChannelPipelineFactory(new StreamServerListenerIMPL(), streamFrameListener));
    

    public static void send(BufferedImage image) throws Exception {
        Object msg = h264StreamEncoder.encode(image);
        channelGroup.write(msg);
    }
    

    来自客户端1的代码:

    public static void init(String host, int port) {
        Webcam webcam = Webcam.getWebcams().get(0);
    
        Dimension sizeVideo = WebcamResolution.QVGA.getSize();
        webcam.setViewSize(sizeVideo);
    
        StreamAtmAgent atmAgent = new StreamAtmAgent(webcam, sizeVideo);
        atmAgent.connect(new InetSocketAddress(host, port));
    }
    

    。 来自客户端2的代码:

    public static void init(String host, int port, ImageListener il) {
        displayWindow.setVisible(true);
        logger.info("Ustawione wymiary :{}", dimension);
        StreamClientAgent clientAgent = new StreamClientAgent(il, dimension);
        clientAgent.connect(new InetSocketAddress(host, port));
    }
    

    你能帮帮我吗?如果您需要更多代码,请告诉我。

    P、 当我在start server中执行类似操作时:

    init("localhost",2000)
    init("localhost",2001)
    

    我用端口2000将client1连接到服务器,并将client2连接到端口2001。我仍然看到2000端口的图像。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Veselin Davidov    6 年前

    我猜您的channelGroup是在所有客户端的所有线程之间共享的?在该集中,您可以添加所有频道—无论它们侦听的是哪个端口:

     Channel channel = serverBootstrap.bind(streamAddress);
     channelGroup.add(channel); //I guess all channels are added here
    

    来自netty文档的Ans here write 方法执行以下操作:

    将指定的消息写入此组中的所有通道。如果 指定的消息是ByteBuf的实例,它会自动 复制以避免竞争条件。这同样适用于 BYTEBUFFHOLDER。请注意,此操作是异步的,因为 频道写入(对象)为。

    所以你基本上在这里 channelGroup.write(msg); 您向所有频道发送相同的消息(图像)。您需要将2000端口的通道与2001端口的通道分开。我认为你甚至不需要一个频道组。只需将端口2000的图像发送到为端口2000创建的通道。