Android Network Discovery Service (ish) before API 14

匆匆过客 提交于 2019-11-30 10:32:26

There are at least three or four options.

You can use

TCP or UDP

  1. Broadcast address
  2. Multicast address
  3. Iterate over all adresses in your network

HTTP

4 Make use of external server or something like GCM (Google Cloud Messaging)

ad.1 and 2

Pros:

+Easy to implement, server is sending predefined "hello" message on broadcast/multicast address of your subnet/network on specific port, client is listening on that port and when "hello" message arrives, he automatically knows server IP address (contained in packet)

+Connection to the Internet is not needed

Cons:

  • Some public networks blocks this addresses to avoid attacks (e.g flooding ).

ad 3.

Pros:

When Multicast and Broadcast is blocked what you can do is simply iterate over whole subnet. It is " brute force " method, but works, especially on typical home/small networks where mask is /24 - there are only 255 addresses to iterate through.

Connection to the Internet also is not needed

Cons

When mask is let say /16 it will take pretty much time to iterate all IPs

can cause battery drain

and will flood network.

ad. 4

Another approach is to make some external server that will be "orientation point" for devices. Each of them sends up its own Address, and looks if there are some entries from another devices from the same network.

Of course Internet connection is needed.

So, I think it is good idea to start with broadcast and multicast since it is very simply and will work in many places, but keep in mind that there are networks where it is not allowed to use.

Good reference can be found here http://docs.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html

You should also check this one http://home.heeere.com/tech-androidjmdns.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!