zy2019
V2EX  ›  Java

请教大佬。关于 netty udp 的技术问题。。

  •  
  •   zy2019 · Jun 24, 2019 · 3033 views
    This topic created in 2544 days ago, the information mentioned may be changed or developed.

    我在一个方法 A 中通过创建的 Bootstrap 对象绑定了本机的 ip 地址,紧接着调用了 writeAndFlush 发送了数据。。但现在的情况是,我在另一个 handler 类中收到了返回值,但是我要继续调用方法 A,可程序会再一次去绑定端口号,导致提示端口被占用。但是我还是想用同一个端口号。 所以,我现在想的是要么找到办法把线程杀死,使在绑定的时候没有被占用。要么是把绑定端口的对象保存起来,可以重复调用。就是不知道怎么弄。 下面是部分 code:

    方法 A 中绑定及发送: ChannelFuture ch = b.bind(8880).sync(); ch.channel().writeAndFlush(new DatagramPacket( Unpooled.copiedBuffer(sendBytes), //地址 new InetSocketAddress(ip, port) )).sync();

    另一个类回调该方法: UdpClient.run(clientIP, clientPort, req); --- 这就是调用方法 A

    其实也没什么特别点的代码,和网上教程的挺像的,但是一直没有找到解决的办法。

    MoHen9
        1
    MoHen9  
       Jun 25, 2019 via Android
    为什么要绑定多次?把 ChannelFuture 保存起来呀,这样下次直接就发了。
    ColoThor
        2
    ColoThor  
       Jun 25, 2019
    把 ChannelFuture 或者 Channel 保存起来
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2801 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:45 · PVG 20:45 · LAX 05:45 · JFK 08:45
    ♥ Do have faith in what you're doing.