InetAddress类和InetSocketAddress类

时间:2023-03-09 13:39:29
InetAddress类和InetSocketAddress类

InetAddress 类

封装计算机的 IP 地址,不包含端口号

InetAddress 类常用的方法

1 String getHostAddress() 获得 IP 地址

2 String getHostName() 获得主机名

3 static InetAddress getByName(String host) 根据主机名获得 IP 地址

 import java.net.InetAddress;
import java.net.UnknownHostException; public class TestInetAddress { public static void main(String[] args) throws UnknownHostException { InetAddress localHost = InetAddress.getLocalHost(); //本机
System.out.println("本机IP地址:" + localHost.getHostAddress());
System.out.println("本机名称:" + localHost.getHostName()); //根据域名得到InetAddress对象
InetAddress bd = InetAddress.getByName("www.baidu.com");
System.out.println("百度服务器地址:" + bd.getHostAddress());
System.out.println("百度服务器名称:" + bd.getHostName()); //根据IP地址得到InetAddress对象
InetAddress ia = InetAddress.getByName("39.130.131.42");
System.out.println("服务器主机IP:" + ia.getHostAddress());
//如果39.130.131.42IP地址不存在或者DNS(域名解析系统)不允许进行IP地址和域名的映射,就会直接返回域名地址
System.out.println("主机名称" + ia.getHostName());
} }

运行结果:

InetAddress类和InetSocketAddress类

-------------------------------------------------------------------------

InetSocketAddress 类

此类用于实现 IP 套接字地址 (IP 地址+端口号),用于socket 通信

InetSocketAddress 类常用的方法

1 InetAddress getAddress() 获取 InetAddress 对象

2 int getPort() 获取端口号

3 String getHostName() 获取主机名

 import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException; public class TestInetSocketAddress { public static void main(String[] args) throws UnknownHostException { //创建对象
InetSocketAddress is1 = new InetSocketAddress("localhost", 9999);
InetSocketAddress is2 = new InetSocketAddress("127.0.0.1", 9999);
InetSocketAddress is3 = new InetSocketAddress("192.168.136.1", 9999); InetAddress ia = InetAddress.getByName("192.168.136.1");
InetSocketAddress is4 = new InetSocketAddress(ia, 9999);
System.out.println("主机名称:" + is4.getHostName());
System.out.println("主机IP地址:" + is4.getAddress());
} }

InetAddress类和InetSocketAddress类

-----------------------------------------------------------------------------------------------------------------------------

URL类

URL(Uniform Resource Locator)统一资源定位符,由 4 部分组成:协议 、存放资源的主机域名、端口号和资源文件名。

URL 是指向互联网“资源”的指针资源可以是简单的文件或目录,也可以是对更为复杂的对象的引用,例如对数据库或搜索引擎的查询

URL 类常用方法

1 String getProtocal() 获取此 URL 的协议名称

2 String getHost() 获取此 URL 的主机名(如果适用)

3 int getPort() 获取 URL 的端口号

4 String getFile() 获取此 URL 的文件名

5 getDefaultPort() 获取与此 URL 关联协议的默认端口号

6 getPath() 获取此 URL 的路径部分

 import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL; public class TestUrl {
public static void main(String[] args) throws MalformedURLException { URL url = new URL("https://www.baidu.com:80/index.html");
System.out.println("协议名称:" + url.getProtocol());
System.out.println("主机名称:" + url.getHost());
System.out.println("端口号:" + url.getPort()); //URL不指明端口号则getPort()返回-1
System.out.println("获取资源路径:" + url.getFile());
System.out.println("获取资源路径:" + url.getPath());
System.out.println("获取默认端口:" + url.getDefaultPort());
} }

InetAddress类和InetSocketAddress类

-------------------------------------------------------------------------------------------------------

openStream()方法   打开到此 URL 的连接并返回一个用于从该连接读入的InputStream

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL; public class TestUrl2 {
public static void main(String[] args) throws IOException {
/**网络爬虫
* (1)从网络上获取资源
* (2)存储到本机
*/
//(1)创建URL对象
URL url = new URL("https://www.baidu.com"); //获取主页资源
//(2)获取字节输入流
InputStream is = url.openStream();
//(3)缓冲流
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
//(4)存储到本地
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F://index.html"), "utf-8"));
//(5)边读边写
String line = null;
while( (line = br.readLine()) != null) {
bw.write(line); //写入
bw.newLine(); //换行
bw.flush(); //清空缓冲区
}
//(6)关闭流
bw.close();
br.close();
} }