This discussion is archived
4 Replies Latest reply: Feb 16, 2007 3:45 AM by 807597 RSS

Problem resolving domain name to current (new) IP address

807597 Newbie
Currently Being Moderated
Hi everyone,

I have a server somewhere in the internet that listens on a certain port. This server is available only through dynamically changing IP addresses therefore the server updates its IP address to a dynamic DNS provider at a regular interval.

I also have a client that connects to this server every minute, retrieving information. This all works very well up to the point where the server gets a new IP address. This is when the client cannot resolve the new IP address to the domain name it uses to create a client Socket. When I shut down the client and start it again after the server IP has changed, it manages to resolve the correct IP address and can connect to the server.

The funny thing is that java version 1.6 manages to ALWAYS resolve the correct IP address to the domain address. Version below, that is 1.5, 1.4.2 etc don't. Versions below java 1.6 seem to cling to the old IP address somewhere in memory even though all my objects are newly instanciated every time. Is the fact that the code below only works correctly after a changing IP address on the server, with java version 1.6 , a new feature? or is it a bug of versions below 1.6.? if not, how do I get the code below to always resolve the correct IP address with java versions below 1.6 (like 1.5.0_11) ?

Note that dig or nslookup always provide the correct and current IP address allocated to "myserver.someDynDNS.org" (domain names / addresses and ports in these examples are made up!)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.Socket;

public class Connect extends Thread {

     public void run() {
          while (true) {
               Socket client = null;
               BufferedReader in = null;
               try {
                    System.out.println("connecting to: "
                              + Inet4Address.getByName("myserver.someDynDNS.org"));
                    client = new Socket(Inet4Address
                              .getByName("myserver.someDynDNS.org"), 60001);
                    in = new BufferedReader(new InputStreamReader(client
                              .getInputStream()));
                    System.out.println(in.readLine());
               } catch (Exception e) {
                    System.out.println(e);
               } finally {
                    try {
                         in.close();
                    } catch (Exception e1) {
                         e1.printStackTrace();
                    }
                    try {
                         client.close();

                    } catch (Exception e) {
                         e.printStackTrace();
                    }
               }
               try {
                    sleep(60000);
               } catch (InterruptedException e) {
                    e.printStackTrace();
               }
          }
     }

}
Message was edited by:
carcophan