Java Socket about DNS Cache

Source: Internet
Author: User

During domain name search through DNS, the specified domain name may be found through multiple intermediate DNS servers. Therefore, it is very expensive to search for a domain name on the DNS server. In Java, DNS cache is provided to alleviate this problem. When the inetaddress class uses a domain name (such as www.csdn.net) to create an inetaddress object for the first time, JVM will combine the domain name with the information (such as IP addresses) it obtains from the DNS) are saved in the DNS cache. When the inetaddress Class re-uses this domain name, it directly obtains the required information from the DNS cache without accessing the DNS server.

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

1. Set the value of networkaddress. cache. TTL in the Java. security. Security. setproperty method in the Program (unit: seconds ). 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 attribute in the Java. Security File. If 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, find the networkaddress. cache. TTL attribute, and set this attribute value to the corresponding cache timeout (unit: seconds ).

If the networkaddress. cache. TTL attribute value is set to-1, the DNS cache data will never be released. The following code demonstrates the effect of using or not using DNS Cache:

Package mynet;

Import java.net .*;

Public class mydns
{
Public static void main (string [] ARGs) throws exception
{
// ARGs [0]: local 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 );
}
}

In the above Code, DNS Cache timeout is set (through the ARGs [1] parameter). You can pass this value to mydns through the command line parameter. This program first uses getallbyname to create an inetaddress array, and then pauses the program through system. In. Read. After a period of time, you can press any key to continue and use the same domain name (ARGs [0]) to create an inetaddress array. If the user waits for a period of time less than the DNS Cache timeout, then no matter how the situation changes, the elements in the addresses2 and addresses1 arrays are the same, in addition, it usually takes 0 ms to create the addresses2 array (Java cannot obtain more accurate time after less than 1 ms ).

Test 1:

Run the following command (set DNS Cache timeout to 5 seconds ):

Java mynet. mydns www.126.com 5

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

Addresses1: 344 Ms
Www.126.com/202.108.9.77
Press any key to continue
Addresses2: 0 ms
Www.126.com/202.108.9.77

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

Addresses1: 344 Ms
Www.126.com/202.108.9.77
Press any key to continue
Addresses2: 484 Ms
Www.126.com/202.108.9.77

Two running results may appear in the test above. If "press any key to continue..." appears ..." After you press any key within five seconds, the running result 1 is displayed. From this result, the addresses2 time is 0 ms, that is, addresses2 does not actually access the DNS server, but directly obtains data from the DNS cache in the memory. When you press any key after five seconds, the running result 2 is displayed. The data in the DNS cache in the memory has been released, so addresses2 has to access the DNS server again. Therefore, the addresses2 time is 484 milliseconds (the number of milliseconds after addresses1 and addresses2 may be different in different environments, but generally, the value of addresses2 in result 1 is 0 or a number close to 0, for example, 5. the value of addresses2 in running result 2 is usually very close to that of addresses1, or a number far greater than 0, such as 1200 ).

Test 2:

Run the following command (computername is the name of the local computer, and DNS Cache timeout is set to never expire [-1]):

Java mynet. mydns computername-1

Running result (delete 192.168.18.20 before pressing any key to continue ):

Addresses1: 31 Ms
Myuniverse/192.168.18.10
Myuniverse/192.168.18.20
Press any key to continue
Addresses2: 0 ms
Myuniverse/192.168.18.10
Myuniverse/192.168.18.20

From the test above, we can see that after the DNS cache is set to never expire, no matter how much time, after pressing any key, addresses2 can naturally get two IP addresses (192.168.18.10 and 192.168.18.20 ), the addresses2 time is 0 ms, but 192.168.18.20 has been deleted. Therefore, it can be determined that addresses2 is the data obtained from the DNS cache. If you run the following command and press any key after five seconds, addresses2 will have only one IP address (192.168.18.10) left ).

Java mynet. mydns computername 5

If the domain name does not exist on the DNS server, the client will throw an unknownhostexception after trying for a period of time (an average of 5 seconds. In order to stop waiting for the next access to this domain name, the DNS Cache also saves this error message. That is to say, only the first attempt to access the wrong domain name is about 5 calls. When you access this domain name again, the unknownhostexception will be thrown directly, without waiting for another 5 seconds,

The reason for the failure to access the domain name may be that the domain name does not exist, or it may be because of a temporary failure of the DNS server or other hardware or software. Therefore, the error message of this domain name cannot be retained. In Java, you can use the networkaddress. cache. Negative. TTL attribute to set the retention time. The default value of this attribute is 10 seconds. It can also be set through the java. security. Security. setproperty method or the java. Security File. The following code demonstrates the networkaddress. cache. Negative. TTL attribute usage:

Package mynet;

Import java.net .*;

Public class mydns1
{
Public static void main (string [] ARGs) throws exception
{
Java. security. Security. setproperty ("networkaddress. cache. Negative. TTL ",
"5 ");
Long time = 0;
Try
{
Time = system. currenttimemillis ();
Inetaddress. getbyname ("www.ppp123.com ");
}
Catch (exception E)
{
System. Out. println ("www.ppp123.com does not exist! Address1 :"
+ String. valueof (system. currenttimemillis ()-Time)
+ "Millisecond ");
}
// Thread. Sleep (6000); // 6 seconds delayed
Try
{
Time = system. currenttimemillis ();
Inetaddress. getbyname ("www.ppp123.com ");
}
Catch (exception E)
{
System. Out. println ("www.ppp123.com does not exist! Address2 :"
+ String. valueof (system. currenttimemillis ()-Time)
+ "Millisecond ");
}
}
}

In the above Code, set the value of networkaddress. cache. Negative. TTL to 5 seconds. This program tests the length of time it takes for address1 and address2 to throw an unknownhostexception when accessing www.ppp123.com (this is a nonexistent domain name, and the reader can replace it with any nonexistent domain name.

Running result:

Www.ppp123.com does not exist! Address1: 4688 Ms
Www.ppp123.com does not exist! Address2: 0 ms

From the above running results, we can see that address2 throws an exception after 0 ms. Therefore, we can conclude that address2 obtains the inaccessible domain name www.ppp123.com from the DNS cache, therefore, an unknowhostexception is thrown directly. If you remove the comments of the delayed code in the above Code, the following running results may be obtained:

Www.ppp123.com does not exist! Address1: 4688 Ms
Www.ppp123.com does not exist! Address1: 4420 Ms

As shown in the preceding running results, the data in the DNS Cache has been released in 6th seconds. Therefore, address2 still needs to access the DNS server to know that www.ppp123.com is an inaccessible domain name.

Note the following when using DNS Cache:

1. You can set the value of the networkaddress. cache. TTL attribute based on the actual situation. Set the value of this attribute to-1. however, if you access a dynamically mapped domain name (for example, you can use the dynamic Domain Name Service to map the domain name to a dynamic IP address of ADSL), the IP address may change, the client still obtains the original IP address.

2. set networkaddress. cache. negative. it is recommended that you do not set the TTL attribute value to-1. Otherwise, if a domain name cannot be accessed due to a temporary fault, when the program accesses this domain name again, even if the domain name returns to normal, the program can no longer access this domain name. Unless you re-run the program.

 

[Transferred from www.bitscn.com]

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.