QUOTE: |
import java.net.InetAddress; import java.net.UnknownHostException; public class NsLookup { static public void main(String[] args) { try { InetAddress address = InetAddress.getByName(args[0]); System.out.println(args[0]+" : "+address.getHostAddress()); } catch(UnknownHostException uhe) { System.err.println("Unable to find: "+args[0]); } } } |
QUOTE: |
bash$ java code.NsLookup www.sun.com www.sun.com: 192.18.97.241 |
QUOTE: |
import java.net.InetAddress; import java.net.UnknownHostException; public class NsLookup { static public void main(String[] args) { try { String name = args[0]; InetAddress[] addresses = InetAddress.getAllByName(name); for(int i=0; i : "+address.getHostAddress()); } } catch(UnknownHostException uhe) { System.err.println("Unable to find: "+args[0]); } } } |
QUOTE: |
try { InetAddress localhost = InetAddress.getLocalHost(); System.out.println ("localhost: "+localhost.getHostAddress()); System.out.println ("localhost: "+localhost.getHostName()); } catch(UnknownHostException uhe) { System.err.println ("Localhost not seeable. Something is odd. "); } |
[火星人 ] 用Java獲得IP地址已經有414次圍觀