Friday, February 14, 2014

Get IP address of a host using InetAddress.getByName() and getAllByName()

using InetAddress.getByName() and getAllByName()

package javaexnetworking;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaExNetworking {
 
 public static void main(String[] args) {
 
 String host = "www.google.com";
 
 System.out.println("by InetAddress.getByName(host):");
 try {
 InetAddress inetAddress = InetAddress.getByName(host);
 System.out.println(inetAddress.toString());
 } catch (UnknownHostException ex) {
 Logger.getLogger(JavaExNetworking.class.getName()).log(Level.SEVERE, null, ex);
 }
 
 System.out.println("\nby InetAddress.getAllByName(host):");
 try {
 InetAddress[] allInetAddress = InetAddress.getAllByName(host);
 for(int i=0; i<allInetAddress.length; i++){
 System.out.println(allInetAddress[i].toString());
 }
 } catch (UnknownHostException ex) {
 Logger.getLogger(JavaExNetworking.class.getName()).log(Level.SEVERE, null, ex);
 }
 }
 
}

No comments:

Post a Comment

[フレーム]

Subscribe to: Post Comments (Atom)

AltStyle によって変換されたページ (->オリジナル) /