Problem resolving domain name to current (new) IP address

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;

publicclass Connectextends Thread{

publicvoid 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

[4112 byte] By [carcophana] at [2007-11-26 18:34:14]
# 1
At least some older Sun VMs (probably 1.4) cached DNS entries including failures. Restarting would fix that.There is some obscure command line option that prevents that.
jschella at 2007-7-9 6:08:21 > top of Java-index,Archived Forums,Socket Programming...
# 2
Also Windows XP had a shocking habit of doing the same thing, for which there is a Registry fix.
ejpa at 2007-7-9 6:08:21 > top of Java-index,Archived Forums,Socket Programming...
# 3

There is a mess of security issues, Windows registry stuff, and a couple of Java properties involved. Start here:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6247501

http://www.rgagnon.com/javadetails/java-0445.html

http://java.sun.com/j2se/1.5.0/docs/guide/net/properties.html

The real fix is to have servers not change their IP addresses.

sjasjaa at 2007-7-9 6:08:21 > top of Java-index,Archived Forums,Socket Programming...
# 4

Ok thanks for the replies, but I've figured out how to solve this problem:

Version prior to 1.6 cache successfull lookups infinitely, and versions from 1.6 cache them only for a few seconds.

I fixed this for java 1.5 by adding a single line of code:

java.security.Security.setProperty("networkaddress.cache.ttl" , "0");

which sets the cche TTL to 0 ie. caching is now disabled. default is -1

carcophana at 2007-7-9 6:08:21 > top of Java-index,Archived Forums,Socket Programming...