package com.snail.example;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostNameExam {
public static void main(String[] args) {
try {
InetAddress addr = InetAddress.getByName("hondou.homedns.org");
byte[] ipaddr = addr.getAddress();
dumpIpAddress(ipaddr);
InetAddress whosAddress = InetAddress.getByAddress(ipaddr);
System.out.println(whosAddress.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static void dumpIpAddress(byte[] bArray){
for( byte b : bArray ){
System.out.print((int)b & 0xff);
System.out.print(" ");
}
System.out.print("\n");
}
}
実行結果
125 197 203 161 fl1-125-197-203-161.tky.mesh.ad.jp
Java 6 から導入された java.net.IDN で、国際化ドメイン名(Unicode)を通常のドメイン名に変換することができる。
package com.snail.example;
import java.net.IDN;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostNameExam {
/**
* @param args
*/
public static void main(String[] args) {
try {
String asciiUrl = IDN.toASCII("日本語.jp");
System.out.println(asciiUrl);
InetAddress addr = InetAddress.getByName(asciiUrl);
byte[] ipaddr = addr.getAddress();
dumpIpAddress(ipaddr);
InetAddress whosAddress = InetAddress.getByAddress(ipaddr);
System.out.println(whosAddress.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public static void dumpIpAddress(byte[] bArray){
for( byte b : bArray ){
System.out.print((int)b & 0xff);
System.out.print(" ");
}
System.out.print("\n");
}
}
実行結果
xn--wgv71a119e.jp 202 11 16 167 jprs.jp