I'm building a music library program, and I want to have the ability to share 开发者_如何学JAVAthe library in the LAN. How can I discover others who share their library? I'd like to find others' libraries without typing in IPs and stuff.
Apple uses mdns (they call it Bonjour) to broadcast and discover music shares on the local network without any user configuration.
Many other manufacturers implement UPnP/AV for the same.
Probably the easiest way is to use UDP to periodically send a message (containing some info advertising your library's presence) to the broadcast address. This will be received by all the hosts on your subnet (and perhaps further, depending on your router configuration). If your app listens for these messages from other hosts, it will over time be able to find all the other instances of your app on the subnet.
Edit: found this question which has answers that go into more specifics of what I'm talking about.
SNMP
This protocol was designed for what you are looking to do. There are several libraries that you could use that implement SNMP which would make it easy to send and receive.
You could get your application to send the fact that the library is shared to a central server - in fact just send the location.
Other instances of your application could then just ask the server for the list of shared libraries.
The advantage of this is that your application isn't constantly broadcasting.
The disadvantage is that you need a central server.
精彩评论