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);
}
}
}
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);
}
}
}
相关文章推荐
- java 网络编程 -- IP地址的表示与网络接口信息的获取(InetAddress和NetworkInterface)
- 使用InetAddress类获取本地计算机的IP地址和主机名
- JAVA中通过InetAddress类获取主机名与IP地址
- 黑马程序员:IP地址:InetAddress类的介绍(无构造函数)
- 正则表达式表示的IP地址
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址
- Java网络编程从入门到精通(10):Inet4Address类和Inet6Address类
- 匹配ip段、ip转换为long型、屏蔽ip段(InetAddress类)
- Java InetAddress类
- 计算机网络: IP地址,子网掩码,网段表示法,默认网关,DNS服务器详解
- IP地址网段表示法
- mysql 使用inet_aton和inet_ntoa处理ip地址数据
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享
- Python IP地址 socket.inet_ntoa unpack/pack
- Python IP地址 socket.inet_ntoa unpack/pack
- Python IP地址 socket.inet_ntoa unpack/pack
- 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法分享
- Java网络编程从入门到精通(10):Inet4Address类和Inet6Address类
- 网络编程:InetAddress类,Socket套接字的使用
- IP地址的三种表示格式及在Socket编程中的应用