问题
I am making a nice multiplayer application but I want my application to search for hosts in you lan. I know that you can obtain your own IP quite easily and then brute force all IPs to see if anything is connected, but this is both quite hard work for your computer and it takes a some time as well even when you multithread it properly.
There seems to be no good way to obtain the arp table in java but I hope there is something similar where you can obtain all local IP-addresses fast and efficient.
Any help would be much appreciated.
回答1:
Several options exists and can be used to achieve this.
The decentralized one
What you could do is broadcasting a request from your application that asks every hosts on the network if they are currently available for a game (sending the request on 255.255.255.255
for instance, will cause your router to retransmit the datagrams you sent to every host on a LAN).
This will ensure that only the hosts that have the game launched and which are ready to play will respond to this request.
Several broadcasting protocols are today heavily used on a LAN. One of them is SSDP (Simple Service Discovery Protocol) which is used with the UPnP protocol for service discovery. There are certainly better suited protocols used over broadcast/multicast for multiplayer games though.
The centralized one
One of the commonest way to discover players on a multiplayer game remains in the presence of a central server on which both requester (people who are looking for a game) and hosters (people who are hosting a game) are connected.
This method has the disadvantage of having a single point of failure, which is the server, but has the advantage, unlike the decentralized solution, of being both suitable for a LAN and for Internet (or every IP based protocol).
If I had to develop a multiplayer game this is where I would start digging regarding the host discovery feature you are required to implement.
来源:https://stackoverflow.com/questions/16326508/getting-all-local-ips-arp-a