1 /*
2 一、网络编程中有两个主要的问题:
3 1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
4 2.找到主机后如何可靠高效地进行数据传输
5
6 二、网络编程的两个要素:
7 1.对应问题一:IP和端口号
8 2.对应问题二:提供网络通信协议:TCP/IP 参考模型(应用层、传输层、网络层、物理+数据链路层)
9
10 三、通信要素一:IP和端口号
11 1.IP:唯一的标识 Internet 上的计算机(通信实体)
12 2.在java中使用InetAddress类代表IP
13 3.IP分类:IPV4和IPV6;万维网和局域网
14 4.域名: www.baidu.com www.mi.com www.sina.com
15 5.本地回路地址:127.0.0.1 对应着:localhost
16
17 6.如何实例化InetAddress:两个方法:getByName(String host)、getLocalHost()
18 两个常用方法:getHostName()/getHostAddress()
19 */
20 public class InterAddressTest {
21 public static void main(String[] args) {
22 try {
23 InetAddress inet1 = InetAddress.getByName("192.168.10.14");//造对象
24 System.out.println(inet1);
25 InetAddress inet2 = InetAddress.getByName("www.atguigu.com");
26 System.out.println(inet2);
27 InetAddress inet3 = InetAddress.getByName("127.0.0.1");//本机的ip地址
28 System.out.println(inet3);
29 InetAddress localHost = InetAddress.getLocalHost();//直接获取本地的ip地址
30 System.out.println(localHost);
31
32 //getHostName() 获取域名
33 System.out.println(inet2.getHostName());
34 //getHostAddress() 获取IP地址
35 System.out.println(inet2.getHostAddress());
36 } catch (UnknownHostException e) {
37 e.printStackTrace();
38 }
39
40 }
41 }