views:

41

answers:

2

Hi, I am writing clients in Java that store the data on a database server. So far, the IP and Port of the server has to be specified in the client's settings manually. I have heard, that it is possible to automatically determine the IP of database servers via broadcast / multicast / UDP (I am not familiar with these concepts). Question: Is there a way to retrieve the IP adresses of all available database-servers in the local network? I am working with the h2 database system so far.

Bye, Wolfgang

A: 

Use UDP broadcasts on the database servers :) This will allow you to pick up the broadcast on all machines on the local network, and the broadcasts themselves can carry the IP address of the servers. For getting the local IP, use InetAddress ip = InetAddress.getLocalHost();

From http://java.sun.com/j2se/1.4.2/docs/api/java/net/DatagramSocket.html:

UDP broadcasts sends are always enabled on a DatagramSocket. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.

Example: DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888)); Which is equivalent to: DatagramSocket s = new DatagramSocket(8888); Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.

Edit: you could also finger all the IP addresses on the network; use the algorithm at http://www.linglom.com/2007/02/20/how-to-find-subnet-number-ip-addresses-in-the-subnet-in-a-few-seconds/ to work out the subnet IPs, iterate over them, and test each for being a H2 server (try connecting). You might want multiple threads to speed up discovery.

Chris Dennett
A: 

You might also want to look into using a Bonjour/Zeroconf library for this purpose, instead of reinventing your own solution for dynamic service discovery.

See http://stackoverflow.com/questions/1233204/are-there-any-other-java-libraries-for-bonjour-zeroconf-apart-from-jmdns

David Gelhar