IP地址:定位电脑
端口定位:软件
URL定位:网络内的每一份资源
传输层协议:TCP/UDP
网络分层:(TCP/IP协议集)应用层、传输层、网络层、网络接口
一、IP常用类:InetAddress
package com.sxt.loc;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class IPTest01 {
public static void main(String[] args) throws UnknownHostException {
//使用getLocalHost方法创建InetAddress对象
InetAddress addr=InetAddress.getLocalHost();
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
//根据域名得到InetAddress对象
addr=InetAddress.getByName("www.shsxt.com");
System.out.println(addr.getHostAddress());
System.out.println(addr.getHostName());
}
}
要点总结:
1、 两个静态方法
getLocalHost:本机
getByName:根据域名解析地址
2、两个成员方法
getHostAddress:返回地址
getHostName:返回名称
二、端口相关类:InetSocketAddress
package com.sxt.loc;
import java.net.InetSocketAddress;
public class PortTest {
public static void main(String[] args) {
InetSocketAddress socketAddress=new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress2=new InetSocketAddress("locahost",9000);
System.out.println(socketAddress.getPort());
System.out.println(socketAddress2.getAddress());
}
}
要点总结:
1、构造器
new InetSocketAddress(地址|域名,端口)
2、方法
getAddress()
getPort()
三、URL相关类:URL
package com.sxt.loc;
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest01 {
public static void main(String[] args) throws MalformedURLException {
URL url=new URL("http://www.baidu.com:80/index.html?uname=shsxt&age=18#a");
System.out.println("协议:"+url.getProtocol() );
System.out.println("端口:"+url.getPort() );
System.out.println("主机名/域名:"+url.getHost());
System.out.println("请求资源1:"+url.getFile());
System.out.println("请求资源2:"+url.getPath());
System.out.println("参数:"+url.getQuery());
System.out.println("锚点:"+url.getRef());
}
}