网络(Internet)

(1)获取IP地址

 1 package JavaEE.JavaBaseExampleTest.Internet;
 2 
 3 import java.net.InetAddress;
 4 import java.net.UnknownHostException;
 5 
 6 /**
 7  * 使用 InetAddress 类的 InetAddress.getByName() 方法来获取指定主机(网址)的IP地址
 8  */
 9 public class GetIP {
10     public static void main(String[] args){
11         InetAddress address = null;
12         try {
13             address = InetAddress.getByName("www.w3cschool.cn");
14         }catch (UnknownHostException e){
15             System.exit(2);
16         }
17         System.out.println(address.getHostName() + "=" + address.getHostAddress());
18         System.exit(0);
19     }
20 }
View Code

猜你喜欢

转载自www.cnblogs.com/skygrass0531/p/12299270.html