Android Network Discovery Service (ish) before API 14

前端 未结 1 1484
误落风尘
误落风尘 2021-01-01 07:16

I need to be able to discover the services on the local network (so say I am running a chat application and I want to discover other devices on local network running this ch

相关标签:
1条回答
  • 2021-01-01 08:00

    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

    0 讨论(0)
提交回复
热议问题