Android 多播套接字停止监听

发布于 2024-09-29 03:11:17 字数 425 浏览 3 评论 0原文

我在 Android 上有一个服务,可以监听并响应通过多播接收的数据包。

这在大多数情况下都有效,但不幸的是,我发现它时不时地会停止接收数据包。线程在调用 MulticastSocket.receive() 时保持阻塞状态,但没有任何结果。我一直在慢慢地尝试找出触发此问题的原因,并且我发现关闭然后再次打开无线或更改无线连接就可以做到这一点。

为了成功地继续侦听我的数据包,我需要关闭 MulticastSocket 并再次重新创建它。然而,我还没有找到任何方法来做到这一点 --- 我似乎没有收到任何发生某些事情的通知:没有意图,没有 Java 异常,什么都没有。如果套接字无效,我希望接收抛出 IOException,但这似乎没有发生。

任何人都可以提供有关(a)实际发生的情况以及(b)如何解决它的任何见解吗?现在我最好的猜测是每隔十秒左右不断地销毁并重新创建套接字,这只是邪恶的。

I have a service on Android that listens and responds to packets it receives via multicast.

This works most of the time, but unfortunately, I'm finding that every now and again it'll just stop receiving packets. The thread remains blocked on the call to MulticastSocket.receive(), but nothing comes out. I've been slowly trying to track down what triggers this, and I've discovered that turning the wireless off and on again, or changing wireless connection will do it.

In order to successfully continue to listen for my packets I need to close the MulticastSocket and recreate it again. However, I haven't found any way of doing this --- I don't appear to get any notification that something's happened: no intents, no Java exceptions, nothing. I would expect the receive to throw an IOException if the socket became invalid, but this appears not to be happening.

Can anyone offer any insight as to (a) what's actually going on, and (b) how to work around it? Right now my best guess is to continually destroy and recreate the socket every ten seconds or so, and that's just evil.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2024-10-06 03:11:17

注意:这个答案在 2010 年提出这个问题时是有意义的,但今天可能已经无关紧要了。

我认为更改无线网络非常类似于禁用和重新启用网络(可能使用不同的 IP 和路由),因此出现问题也就不足为奇了。

也许注册网络信息的更改会给您带来所需的警告?

http://developer.android.com/reference/android/net/ ConnectivityManager.html#CONNECTIVITY_ACTION

和相关页面

Note: This answer was meaningful when the question was asked in 2010, it is probably irrelevant today.

I would think that changing wireless networks is pretty much akin to disabling and re-enabling the network (likely with a different IP and routing), so no surprise that things break.

Perhaps registering for changes in the network info would give you the needed warning?

http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION

and associated pages

℡寂寞咖啡 2024-10-06 03:11:17

您是否正在获取多播锁?

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = manager.createMulticastLock("lock name");
lock.setReferenceCounted(true);
lock.acquire();

某些设备似乎也会阻止多播:

Android 2.2 上的多播

Are you acquiring a multicast lock?

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = manager.createMulticastLock("lock name");
lock.setReferenceCounted(true);
lock.acquire();

It also seems like certain devices block multicast:

Multicast on Android 2.2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文