Java network programming from getting started to mastering (4): DNS Caching

Source: Internet
Author: User
Tags continue

In the process of locating a domain name through DNS, it is possible to pass through several intermediate DNS servers to find the specified domain name, so locating the domain name on the DNS server is a very expensive operation. In Java, DNS caching is provided to mitigate this problem. When the InetAddress class creates a InetAddress object for the first time with a domain name, such as Www.csdn.net, the JVM saves the domain name and the information it obtains from DNS, such as IP addresses, in the DNS cache. The next time the InetAddress class uses this domain name, it obtains the required information directly from the DNS cache without having to access the DNS server again.

DNS caching will always retain the domain name information that has been accessed by default, but we can modify this default value. There are generally two ways to modify this default value:

1. Set the value of the Security property Networkaddress.cache.ttl (in seconds) in the program through the Java.security.Security.setProperty method. The following code sets the cache timeout to 10 seconds:

Java.security.Security.setProperty ("Networkaddress.cache.ttl", 10);

2. Set the Networkaddress.cache.negative.ttl property in the Java.security file. Assuming that the JDK installation directory is C:\jdk1.6, the Java.security file is located in the C:\jdk1.6\jre\lib\security directory. Open this file, locate the Networkaddress.cache.ttl property, and set this property value to the appropriate cache timeout (in seconds).

If the Networkaddress.cache.ttl property value is set to-1, then the DNS cache data will never be freed. The following code demonstrates the effect of using and not using DNS caching:

Package mynet;

Import java.net.*;

public class Mydns
{
public static void Main (string[] args) throws Exception
{
Args[0]: Native name args[1]: Buffer time
if (Args.length < 2)
Return
Java.security.Security.setProperty ("Networkaddress.cache.ttl", args[1]);
Long time = System.currenttimemillis ();
InetAddress addresses1[] = Inetaddress.getallbyname (args[0));
System.out.println ("Addresses1:"
+ string.valueof (System.currenttimemillis ()-time)
+ "millisecond");
for (inetaddress address:addresses1)
SYSTEM.OUT.PRINTLN (address);
System.out.print ("Press any key to continue");
System.in.read ();
Time = System.currenttimemillis ();
InetAddress addresses2[] = Inetaddress.getallbyname (args[0));
System.out.println ("Addresses2:"
+ string.valueof (System.currenttimemillis ()-time)
+ "millisecond");
for (inetaddress Address:addresses2)
SYSTEM.OUT.PRINTLN (address);
}
}

The DNS cache timeout (through args[1] parameter is set in the above code, and the user can pass this value into the Mydns through command-line arguments. This program first uses Getallbyname to build a inetaddress array and then pauses the program by System.in.read. When the user waits for a period of time, you can press any key to continue and use the same domain name (args[0]) to create a inetaddress array. If the user waits longer than the DNS cache timeout, the elements in the Addresses2 and addresses1 arrays are the same regardless of the circumstances, and the time spent creating the Addresses2 array is typically 0 milliseconds (less than 1 milliseconds later Java does not get more precise time.

Test 1:

Execute the following command (set the DNS cache timeout to 5 seconds):

Java mynet. Mydns www.126.com 5

Run result 1 (press any key within 5 seconds):

addresses1:   344毫秒
www.126.com/202.108.9.77
按任意键继续
addresses2:  0毫秒
www.126.com/202.108.9.77

Run result 2 (press any key after 5 seconds):

addresses1:   344毫秒
www.126.com/202.108.9.77
按任意键继续
addresses2:  484毫秒
www.126.com/202.108.9.77

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.