자바 로 IP 주소 가 져 오기 (정리)

2151 단어
package ipDemo;

import java.io.File;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;


public class GetIpAddress {
 public static void main(String[] args) {
  if(args.length==0){
   printLocalInfo();
  }else{
   printInputInfo(args);
  }
 }
 /**
  *    IP 
  */
 static void printLocalInfo(){
  try {
   Enumeration<NetworkInterface> interfaces 
    = NetworkInterface.getNetworkInterfaces();
   if(interfaces == null){
    System.out.println("There isn't network interface!");
   }else{
    while(interfaces.hasMoreElements()){
     NetworkInterface interInfo = interfaces.nextElement();
     System.out.println("Interface:"+interInfo.getName()+"--");
     Enumeration<InetAddress> addressEnum = interInfo.getInetAddresses();
     if(!addressEnum.hasMoreElements()){
      System.out.println("\t(No address for this interface)");
     }
     while(addressEnum.hasMoreElements()){
      InetAddress address = addressEnum.nextElement();
      String str =null;
      if(address instanceof Inet4Address){
       str = "(IPv4)";
      }else if(address instanceof Inet6Address){
       str = "(IPv6)";
      }else{
       str = "(?)";
      }
      System.out.print("\tAddress "+str);
      System.out.println(File.separator+address.getHostAddress());
     }
    }
   }
  } catch (SocketException se) {
   System.out.println("NetWork interface error:"+se.getMessage());
  }
 }
 /**
  *       IP 
  */
 static void printInputInfo(String[] args){
  for(String host:args){
   try {
    System.out.println(host+":");
    InetAddress[] addressArray = InetAddress.getAllByName(host);
    for(InetAddress address:addressArray){
     System.out.println("\t"+address.getHostName()+File.separator
       +address.getHostAddress());
    }
   } catch (UnknownHostException e) {
    System.out.println("\tUnable to find address for "+host);
   }
  }
 }
}

좋은 웹페이지 즐겨찾기