2007年10月1日 星期一

取得localhost ip及轉換DNS TO IP

package com.webtest;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author jimmy
*/
public class ip {

public String getip(){ //local ip
String localip = "";
try{
InetAddress address=InetAddress.getLocalHost();
localip=address.getHostAddress();
} catch(UnknownHostException uhe){
localip = "Unable to find: ";
}
return localip;
}

public String DNS2ip(String hostname){ // server ip
String strvar="";
try{
InetAddress address=InetAddress.getByName(hostname);
strvar=address.getHostAddress();
} catch(UnknownHostException uhe){
strvar="Unable to find: "+hostname;
}
return strvar;
}

static public void main(String args[]){
ip a=new ip();
//System.out.println(aa.getip(args[0]));
System.out.println(a.getip());
System.out.println(a.DNS2ip("www.yahoo.com.tw"));
}

}

沒有留言: