獲取IP地址

package com.tianren.service;
import java.net.*;

import sun.security.krb5.internal.HostAddress;

public class Test{
InetAddress myIPaddress = null;
InetAddress myServer = null;
String hostAddress = null;
String hostName = null;
String Address = null ;
String Name = null ;
public static void main(String args[]) {

Test mytool;
mytool = new Test();
mytool.getAllServerIP();
// System.out.println("Your host IP is: " + mytool.getMyIP());
// System.out.println("The Server IP is :" + mytool.getServerIP());
}

// 取得LOCALHOST的IP地址
public String getMyIP() {
try {
myIPaddress = InetAddress.getLocalHost();
hostAddress = myIPaddress.getHostAddress();//僅僅獲得IP地址
hostName = myIPaddress.getHostName();//獲得本地名稱
} catch (UnknownHostException e) {
}
return (hostName);
}

// 取得 www.abc.com 的IP地址
public String getServerIP() {
try {
myServer = InetAddress.getByName("www.abc.com");
Address = myServer.getHostAddress();//獲得www.abc.com的ip地址
Name = myServer.getHostName();//獲得域名
} catch (UnknownHostException e) {
}
return (Name);
}
//獲取此域名下的所有IP
public void getAllServerIP(){
String name = "www.microsoft.com";
try{

InetAddress[] addresses= InetAddress.getAllByName(name);
for(int i =0;i<addresses.length;i++){
System.out.println(addresses[i].getHostAddress());
}
}catch(Exception e){
System.err.println("Unable to find: "+name);

}
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章