gpt4 book ai didi

android - 是什么导致多播消息在 wifi 重启后不立即流动

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:17 25 4
gpt4 key购买 nike

我有一个 Android 应用程序可以创建 MulticastSocket、加入 MC 组并从本地 wifi 网络上的另一台机器接收消息。

MulticastSocket socket = new MulticastSocket(null); // Create an unbound socket.
socket.setSoTimeout(LISTEN_TIMEOUT_MILLIS);
socket.setReuseAddress(true);
socket.bind(new InetSocketAddress(listenPort)); // Bind to the configured multicast port

final WifiManager.MulticastLock lock = wifiManager.createMulticastLock("my_lock");
lock.acquire();

socket.setNetworkInterface(networkInterface);
socket.joinGroup(multicastGroup);
while (true) {
socket.receive(packet);
// Do something with the packet
// Handle timeout etc.
// Handle change of network interface by leaving group, setting netIntf and joining group again.
}
socket.leaveGroup(multicastGroup);
socket.close();

lock.release();

在大多数 Android 设备(华为、三星)上运行良好,但在某些设备 (Pixel3) 上,如果设备上的 WiFi 关闭然后重新打开,当应用程序看到 Wifi 连接上线时,它可能需要最多 14 分钟(变化很大),然后才开始再次接收 MC 消息。

即使丢弃 Socket 并创建一个新的 MCSocket 也不能​​减轻延迟。

但它必须是 JVM 中保存的某种状态,因为应用程序的重启会导致它立即连接。

感觉好像有一些租约正在为 MC 连接保留,只在一个时钟周期内更新。

所以我的问题是:

  1. 是什么导致 MC 消息在WiFi 连接恢复并创建一个新的 MCSocket听这个。
  2. 我该怎么做才能确保及时恢复消息流?

最佳答案

我注意到您已经更新了您的问题以包含 WifiManager.MulticastLock

我想知道您是否在 Wifi 连接恢复时重新获取锁,SO 上的一些帖子暗示这是必要的。

我注意到以下帖子的评论:

回复:https://stackoverflow.com/a/4002084/1015289

it turns out that your multicast lock is destroyed when the connectivity goes away (the long delay was me rewriting my code three times before I figured this out). So, you have to reacquire the lock every time the connection comes back

关于android - 是什么导致多播消息在 wifi 重启后不立即流动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53967345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com