-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic_method.java
More file actions
31 lines (26 loc) · 1.07 KB
/
Basic_method.java
File metadata and controls
31 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package Github.url;
import java.net.*;
/**
* 利用InetAddress的三种方法创建InetAddress对象
* 1)getLocalHost();
* 2)getByName("www.biying.com");//根据域名得到InetAddressz对象
* 3)getByName("61.135.253.15");//根据ip得到InetAddress对象
* @author xiaohong
*
*/
public class Basic_method {
public static void main(String[] args) throws UnknownHostException{
//使用getLocalHost()方法创建对象InetAddress
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostName());//返回本机的计算机名
System.out.println(address.getHostAddress());//返回本机的IP地址
//根据域名得到InetAddressz对象
address = InetAddress.getByName("www.biying.com");
System.out.println(address.getHostName());//输出:www.biying.com
System.out.println(address.getHostAddress());//返回必应服务器的ip
//根据ip得到InetAddress对象
address = InetAddress.getByName("61.135.253.15");
System.out.println(address.getHostName());//输出的时IP而不是域名,如果这个IP不存在,则返回LocalHost
System.out.println(address.getHostAddress());//返回163服务器的IP:61.135.253.15
}
}