要素一.IP地址和端口号
IP地址
package com.jk.java;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*
* 网络通信的第一个要素:IP地址。通过IP地址,唯一的定位互联网上一台主机
* InetAddress:位于java.net包下
* 1.InetAddress用来代表IP地址,一个InetAddress的对象代表着一个IP地址
* 2.如何创建InetAddress的对象,getByName(String host)
* 3.getHostName():获取IP地址对应的域名
* getHostAddress();获取IP地址
*/
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
//创建一个InetAddress对象:getByName()
InetAddress inet = InetAddress.getByName("www.baidu.com");
System.out.println(inet);
//两个方法
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
//获取本机的IP:getLocalHost()
InetAddress inet1 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());
}
}
端口号:标识在计算机上运行的进程(程序)
>不同的进程有不同的端口号
>被规定为一个16位的整数0~65535,其中0~1023被预先定义的服务通信占用。
端口号和IP地址的组合得出一个网络套接字
要素二..网络通信协议