• Post Reply Bookmark Topic Watch Topic
  • New Topic

InetAddress "cache" reload  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I've got a problem here...
In a Threaded program a module connects to a server. However, if the client computer is not connected to the network ast the moment the InetAddress throws a HostNotFoundException - which is fine. But if the client connects to the network after the failed attempt, the InetAddress continues to throw HostNotFoundExceptions and I figure the resource is cached somewhere since there is no references in my code between the different connection attempts. Can I force the InetAddress to do a new lookup?
Yours
- Lars J. Nilsson
 
maateen
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would u plz send ur code
it may help me to figure out ur prob...
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, 2000+ lines would be a little extensive to post here, wouldn't it? However, a run down might look like this:

public class Mailer {
// graphic mail application using the main thread - upon
// mail sending it calls it's own method "sendMail"
private void sendMail() {
Thread th = new Thread(new Sender());
th.start();
}
private class Sender() implements Runnable {
// inner class Sender uses a "SMTPSender" object to
// connect and send mails
public void run() {
SMTPSender sender = null;
try {
sender = new SMTPSender();
// use sender
} catch(MailException e) { //...
} finally {
if(sender != null) sender.disconnect();
}
}
}
}
public class Sender {
// class Sender connects to a SMTPServer and resolves a
// email object unto the servers inputstream
private Socket socket;
// the socket is class member, it connects with a
// connect method and disconnects like this
public void disconnect() {
try {
// close stream and son on...
socket.close();
} catch(IOException e) { //...
} finally {
socket = null;
}
}
}

Simplyfied, but that's about it. I've tried to create an InetAddress for the socket myself but it didn't help. My first thought was that it was a optimization by the compiler so I tried placing "volatile" here and there in the code. Didn't help. I've forced the System.gc(). Didn't help. I'm out of ideas.
Yours
- Lars
 
Johan Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a known problem, the JDK is caching all dns-lookups, and it never expires them.... However, if you're on Java 2, there's an undocumented property you can set, 'sun.net.inetaddr.ttl', it's set to -1 by default (which means never expire), 0 means don't cache ay all, and any value > 0 means cache for x seconds. You would set this property by passing in the following commandline argument to the VM when you start it: -Dsun.net.inetaddr.ttl=<x>
/Johan/
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers and thank you. By now I've implemented my own MX and A DNS lookup, but for simpler apps. this will probaly help a lot.
- Lars J. Nilsson
http://www.larsan.net/java/
 
Rajal Mistry
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Johan,
I have seen and tested your solution for Nilssons problem of cache reload.It works absolutely fine when a system is not on network and brought on network . but once the system comes on network i figuired that cache is again full time active and InetAddress goes to read that continuosly ,,so, now if the system goes out of network then what is the solution i mean how to get the msg system is not on n/w from the code below.
i the below written program by saying
java -Dsun.net.inetaddrr.ttl=1 InetAddressTest
import java.net.*;

public class InetAddressTest
{
public static void main(String args[])
{
SystemsOnNetwork sysonnet=new SystemsOnNetwork();
try
{
sysonnet.th.join();
}catch(Exception ie)
{
}

}
}
class SystemsOnNetwork implements Runnable{

Thread th;
String curr = "200.100.50.15";

SystemsOnNetwork()
{
th = new Thread(this);
th.start();
}

public void run()
{
while (true)
{
try{
InetAddress Inet = InetAddress.getByName (curr);
System.out.println(" System is on n/w");
}catch(UnknownHostException uhe){
System.out.println(" System is not on n/w");
}
}
}

}//class

Originally posted by johan:
This is a known problem, the JDK is caching all dns-lookups, and it never expires them.... However, if you're on Java 2, there's an undocumented property you can set, 'sun.net.inetaddr.ttl', it's set to -1 by default (which means never expire), 0 means don't cache ay all, and any value > 0 means cache for x seconds. You would set this property by passing in the following commandline argument to the VM when you start it: -Dsun.net.inetaddr.ttl=<x>

/Johan/

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!