Local network peer discovery

It’s a class for finding other computers running the same code - useful for finding game servers etc. Can use multicast or broadcast UDP, so it probably won’t reach farther than the local LAN subnet, but it beats typing in IP numbers

Code is here, JavaDoc is here. Usage looks like:


// constructs multicast-based peer
PeerDiscovery mpd = new PeerDiscovery( multicastGroupIP, port, packetTTL );

// constructs broadcast-based peer
PeerDiscovery bpd = new PeerDiscovery( groupIP, port);

// queries the group, and waits for responses
InetAddress[] peers = peer.getPeers( timeout );

// when you're done...
peer.disconnect();

Discovery is reactive i.e.: peers listen for and respond to a query packet. In multicast mode, all traffic is multicast. In broadcast mode, the query packet is broadcast while the responses are unicast.

Multicast vs Broadcast - Distilled wisdom from the replies to this thread: Multicast is theoretically more efficient, and so may be allowed to propogate beyond the local subnet (will not go over the internet), but requires more router complexity and so is not terribly well supported. Broadcast is simple, so will work everywhere, but is unlikely to ever reach beyond the local subnet. Upshot: Use broadcast.

There’s an executable jar here if you want to check the behaviour of your network.

edit: added broadcast mode, tested that it actually works, summarised thread.

Ah, I posted something similar here earlier, and it is tested:
http://www.java-gaming.org/index.php/topic,19130.msg150823.html#msg150823

Yeah, I had spotted that, but I thought I’d give multicast a try. Do you have any feeling on how well multicast is supported, as opposed to broadcast? I haven’t been able to find much concrete information, just vague mutterings that multicast might at some point be allowed to reach further than the local subnet, but it isn’t terribly well supported yet.
Hmmm, I might well stick some broadcasting up in this thing…

That will cause the internets to be flooded pretty easily. Everybody would use it as an extremely cheap way of streaming video – the routers will be swamped as they keep duplicating data every hop.

Agreed, I wasn’t envisaging multicast working over the internet, just that multicast might eventually reach further than broadcast on non-domestic networks. Case in point: multicast support is planned for the network where I work, but not for a while :frowning:

I was trying to figure out what you’re both talking about and found this article which helped:
http://episteme.arstechnica.com/eve/forums/a/tpc/f/469092836/m/4610971075

Still, I’m confused as to which is better for server discovery. That article makes it sounds like broadcast is better since it always goes everywhere.

In the end it doesn’t really matter. Broadcast just does the job, and appearantly multicast too. Multicast should be more efficient, but that’s up to the switch, which might just as well send the packet to everybody, like how a hub works. Maybe professional switches do a better job.

In the end efficiency doesn’t really matter, as your server discovery traffic isn’t going to saturate the network any time soon.

I’ve added a broadcast-UDP mode, and actually tested it now (it works!). Edits in the first post.

FWIW, here is my implementation:
http://code.google.com/p/kryonet/source/browse/trunk/kryonet/src/com/esotericsoftware/kryonet/Client.java#330

Offtopic:

You might know that Linus Torvald once spoke the wise words:
‘If you use more than 3 levels of indentation, you’re screwed anyway.’

Heh. Tell that to try/catch/finally. You do use Java, right? :stuck_out_tongue:

Offtopic:

Check my network API, I rarely go beyond 3 levels (within a method body)

Yeah. I tend to try to avoid indentation by returning, avoiding unnecessary else, etc. I usually don’t bother adding methods just to avoid it though.

Everyone has widescreen 16:9, or 16:10 monitors now-a-days, so it’s more like 4 levels of indent ;D

He has 9!! :o

Who needs maintainability if you decide not to write bugs!

There are more important things to worry about. More than 3 tabs certainly doesn’t make code unmaintainable.

I forgot the name, but there is a metric for code complexity that uses the number of execution paths for a method. If you have 9 levels of indent, the number of possible paths becomes very high.