Discovering clients on a wifi network

前端 未结 4 1320
不知归路
不知归路 2021-01-23 16:34

I\'m writting a java application, and I need to quickly discover any other running clients on any wired or wireless local networks in order to establish a TCP connection.

<
4条回答
  •  一生所求
    2021-01-23 17:12

    Multicast UDP is a good way of doing this. It's used in a couple of technologies that support automatic discovery of networked devices over local IP networks (UPnP and ZeroConf).

    Multicast UDP is not TCP, but it is still based on IP and, so, uses the same addressing mechanism i.e. IP addresses. Quite often it is compared to radio broadcasting i.e. a multicast sender only needs to send 1 message (i.e. it is like a broadcast) but only clients that are "tuned-in" to the multicast channel will receive it.

    You can do a quick search on google or wikipedia for these as a starter, but the basic idea is as follows:

    • when a client starts, it sends out a multicast UDP "hello" message to some pre-specified multicast address and port (e.g. UPnP uses 239.255.255.250:1900)
    • existing clients are listening for incoming multicast "hello" messages on the specified address and port - when a client receives one, it sends a response to the sender
    • the client sending the "hello" message receives a response from each existing client on the network and is now aware of the IP address of each client

    If you are looking for libraries to use, UPnP libraries can tend to be a bit heavyweight and a lot of folk generally don't like them, so ZeroConf might be a little more suitable. I don't know of any java implementations of such things but I'm sure you can find some with a little digging around.

提交回复
热议问题