import java.net.InetAddress;
import java.net.UnknownHostException;
//测试ip
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机ip地址
System.out.println("查询本机ip地址:");
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
System.out.println("-----------------------------------");
//查询网络ip地址
System.out.println("查询网络ip地址:");
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
System.out.println("-----------------------------------");
//常用方法
System.out.println("常用方法:");
System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
System.out.println(inetAddress4.getHostAddress());
System.out.println(inetAddress4.getHostName());
} catch (UnknownHostException e) {
System.out.println("111");
}
}
}
(网络编程)InetAddress(表示ip地址的类)相关使用
最新推荐文章于 2022-06-20 20:14:32 发布