views:

103

answers:

2

Hi all, This is what I'm planning to do. Suppose there are 2 machines A and B. I need to create an application by which machine A can send data to Machine B, using Java SE.

I know that to make a connection you need to create an object of serversocket class and use the accept() method as

In Machine B: ServerSocket ss=new ServerSocket(12000);

while(true)

{

Socket s=ss.accept();

      .
      .
      .
      .

}

In Machine A:

Socket s=new Socket("<Machine B's IP Address>",12000);

                .
                .
                .
                .

Here's the problem.

Suppose if Machine B's IP address is assigned dynamically with DHCP, ie it can change each time the machine reboots.

Is there any way by which the Java Pgm in machine A can recognize the IP address of B without the user typing it manually???

Hoping that you the question is clear. Waiting for replies!

+3  A: 

Most DNS servers have a mechanism that allows the DHCP server to make dynamic updates to the local (internal) zone. If you have this feature at your disposal, then you can just use DNS names, and let the systems guys make sure that the DNS records are up-to-date with the DHCP leases.

A similar DNS-based approach could be accomplished on the wider internet using a DDNS service like dyndns.

If you can't use DNS, then you're stuck with a UDP broadcast-based approach. This is the same mechanism that protocols like DHCP, bonjour, and SMB use to find other computers on the local network without knowing their addresses; and this approach will only work in the same contexts where those technologies work (typically, only within the local network, and definitely not across the greater internet).

In java, you would accomplish UDP broadcast messaging via the DatagramSocket class.

Quoting the api docs:

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.\

If your two machines are located at disparate locations on the internet, and you can't use a DDNS service (like dyndns, mentioned above), then your best bet is to build a central registration server. This would be a simple server (could even be a web service) that runs at a known address on the internet. Each of your client machines would register with the central registration server, thus the central registration server would know the IP addresses of both machines. Then each machine could request the other's address via the central registration server. You'd have to put some thought into to security in order to protect against malicious "poisoning" of your registry, but otherwise, this would be fairly straight forward.


EDIT:

I just discovered JXTA. It's a set of tools for building java applications with peer-to-peer networking features. I don't have any direct experience with it, but it looks like it would accomplish what you're looking for (and probably save you a lot of work over having to build it all from scratch).

In fact, it looks like the subproject p2psockets does exactly what you want.

Lee
@Lee Thanks a lot for the help!! :) :) I'm planning to implement this system across our college LAN. So i dont need to look into DNS, i guess. The other 2 alternatives are applicable. Either i can put up an Application in the College's main server and in can contact that server frm A and B first. Or i can use the UDP broadcast. The latter will be easy to implement and much less complex i guess. The machines first broadcasting their IP addresses across the network will be less tedious. Can you just help me on how to broadcast a UDP packet??
Nabeel
Should i send it to the broadcast address??? like x.x.x.255?
Nabeel
yes - send it to the [broadcast address](http://en.wikipedia.org/wiki/Broadcast_address). but be aware that the proper broadcast address will change depending on the topology of your network. [Broadcast Address Calculator](http://www.tech-faq.com/calculate-broadcast-address.html) -- nothing is ever easy. ;) Also, see the edit I just made regarding JXTA - this looks like a *much* easier solution than UDP Datagrams.
Lee
+1  A: 

In Java you can compute the IP address of machine by host name, Here is the code

java.net.InetAddress inetAdd = java.net.InetAddress.getByName("www.ibm.com"); System.out.println ("IP Address is : " + inetAdd.getHostAddress());

Use this code to get the changed IP address every time machine reboots

JRomio