Android的多播套接字停止侦听 [英] Android Multicast socket stops listening

查看:269
本文介绍了Android的多播套接字停止侦听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个侦听和响应报文的通过多播接收在Android的服务。

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

这工作的大部分时间,但不幸的是,我发现现在每一次它会只是停止接收数据包。 )阻塞调用MulticastSocket.receive(线程遗体,但没有出来。我一直在慢慢地试图追查什么触发这​​一点,我发现,打开无线关闭并再次,或改变无线连接将做到这一点。

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.

为了成功地继续听我的包,我需要关闭的MulticastSocket,再重新创建它。但是,我还没有发现这样做的任何方式---我似乎没有得到的东西的发生任何通知:没有意图,没有Java异常,什么都没有。我希望接收到抛出IOException如果套接字变得无效,但这似乎没有发生。

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.

任何人都可以提供任何见解,以(一)什么是真正回事,和(b)如何解决呢?现在我最好的猜测是不断破坏并重新创建插座每十秒钟左右,这只​​是邪恶的。

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.

推荐答案

我认为变化的无线网络是pretty很多类似于禁用和重新启用网络(可能具有不同的IP和路由),所以毫无疑问,事情打破。

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?

<一个href=\"http://developer.android.com/reference/androi/net/ConnectivityManager.html#CONNECTIVITY_ACTION\" rel=\"nofollow\">http://developer.android.com/reference/androi/net/ConnectivityManager.html#CONNECTIVITY_ACTION

和相关页面

这篇关于Android的多播套接字停止侦听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆