Created
May 23, 2013 18:35
-
-
Save ecki/5638362 to your computer and use it in GitHub Desktop.
Java Networking 501 :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.net.InetAddress; | |
import java.net.InetSocketAddress; | |
import java.net.UnknownHostException; | |
/** | |
* Demonstrate that InetAddress#getByName() with a literal does not | |
* fill in the host name, which leads to (uncached) reverse lookups | |
* on InetAddress#getHostName(). | |
* <P> | |
* This can be avoided by not using getHostName() or by creating the | |
* Object with InetAddress#getByAddr(String, byte[]). | |
* | |
* @author Bernd Eckenfels | |
*/ | |
public class AvoidReverseTest | |
{ | |
public static void main(String[] args) throws UnknownHostException | |
{ | |
long start; InetAddress addr = null; InetSocketAddress sockAddr = null; String dummy = null; | |
for(int i=0; i < 3; i++) | |
{ | |
start = System.nanoTime(); | |
addr = InetAddress.getByName("176.52.246.130"); | |
sockAddr = new InetSocketAddress(addr, 80); | |
dummy = sockAddr.getAddress().getHostName(); dummy.toString(); | |
System.out.println("byName+getHostName Difference " + ((System.nanoTime() - start) / (1000000.0)) + "ms"); | |
} | |
System.out.println("byName+getHostName: " + dummy); | |
System.out.println(); | |
for(int i=0; i < 3; i++) | |
{ | |
start = System.nanoTime(); | |
addr = InetAddress.getByName("176.52.246.130"); | |
sockAddr = new InetSocketAddress(addr, 80); | |
dummy = sockAddr.getAddress().toString(); dummy.toString(); | |
System.out.println("byName+toString Difference " + ((System.nanoTime() - start) / (1000000.0)) + "ms"); | |
} | |
System.out.println("byName+toString: " + dummy); | |
System.out.println(); | |
for(int i=0; i< 3; i++) | |
{ | |
start = System.nanoTime(); | |
byte[] b = new byte[4]; b[0]=(byte)176; b[1]=(byte)52; b[2]=(byte)246; b[3]=(byte)130; | |
addr = InetAddress.getByAddress("176.52.246.130", b); | |
sockAddr = new InetSocketAddress(addr, 80); | |
dummy = sockAddr.getAddress().getHostName(); dummy.toString(); | |
System.out.println("byAddress+getHostName Difference " + ((System.nanoTime() - start) / (1000000.0)) + "ms"); | |
} | |
System.out.println("byAddress+getHostName: " + dummy); | |
System.out.println(); | |
for(int i=0; i< 3; i++) | |
{ | |
start = System.nanoTime(); | |
byte[] b = new byte[4]; b[0]=(byte)176; b[1]=(byte)52; b[2]=(byte)246; b[3]=(byte)130; | |
addr = InetAddress.getByAddress("176.52.246.130", b); | |
sockAddr = new InetSocketAddress(addr, 80); | |
dummy = sockAddr.getAddress().toString(); dummy.toString(); | |
System.out.println("byAddress+toString Difference " + ((System.nanoTime() - start) / (1000000.0)) + "ms"); | |
} | |
System.out.println("byAddress+toString: " + dummy); | |
} | |
/* Output: | |
byName+getHostName Difference 4504.681589ms | |
byName+getHostName Difference 4499.818737ms | |
byName+getHostName Difference 4500.038486ms | |
byName+getHostName: 176.52.246.130 | |
byName+toString Difference 0.088443ms | |
byName+toString Difference 0.059465ms | |
byName+toString Difference 0.057352ms | |
byName+toString: /176.52.246.130 | |
byAddress+getHostName Difference 0.019923ms | |
byAddress+getHostName Difference 0.006339ms | |
byAddress+getHostName Difference 0.006339ms | |
byAddress+getHostName: 176.52.246.130 | |
byAddress+toString Difference 0.025959ms | |
byAddress+toString Difference 0.022639ms | |
byAddress+toString Difference 0.022035ms | |
byAddress+toString: 176.52.246.130/176.52.246.130 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment