您的位置:首页 > 其它

InetAddress类表示IP地址

2009-03-08 21:37 246 查看
package getIpAddress;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetByName {
public static void main(String[] args){

String hostAddress="www.google.cn";

//InputStreamReader可以将二进制流转化为字符流,使用最为频繁。
BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in));

try {
hostAddress=bfr.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}

try {
//InetAddress没有公共构造函数
InetAddress net=InetAddress.getByName(hostAddress);
//获得地址组
InetAddress[] addrs=InetAddress.getAllByName(hostAddress);

System.out.println(net);
//result:www.google.com/64.233.189.104
for(int i=0;i<addrs.length;i++){
System.out.println(addrs[i]);
}
//result:www.google.com/64.233.189.104
// www.google.com/64.233.189.147
// www.google.com/64.233.189.99

InetAddress inet=InetAddress.getLocalHost();
System.out.println(inet);
//result:fdu-hjh/10.11.13.128

} catch (UnknownHostException e) {
System.out.println("Cann't find host:"+hostAddress);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: