网络
- IP地址 每位8个1,最大255,本地回环地址:127.0.0.1
- 端口 用于标识进程的逻辑地址 0-1024系统使用 0-65535
InetAddress InetSocketAddress
- InetAddress 封装计算机的 IP 地址,不包含端口号
- InetSocketAddress 此类用于实现IP套接字地址(IP 地址+ 端口号),用于socket
/*InetAddress*/
InetAddress ia = InetAddress.getByName("localhost");
System.out.println(ia.getHostName()); //localhost
System.out.println(ia.getHostAddress()); //127.0.0.1
InetAddress ia2 = InetAddress.getLocalHost();
System.out.println(ia2.getHostName()); //SC-201901080146
System.out.println(ia2.getHostAddress()); //192.168.0.108
InetAddress ia3 = InetAddress.getByName("127.0.0.1");
System.out.println(ia3.getHostName()); //activate.navicat.com
System.out.println(ia3.getHostAddress()); //127.0.0.1
InetAddress ia4 = InetAddress.getByName("www.baidu.com");
System.out.println(ia4.getHostName()); //www.baidu.com
System.out.println(ia4.getHostAddress()); //180.97.33.107
/*InetSocketAddress*/
InetSocketAddress isa = new InetSocketAddress("localhost", 80);
System.out.println(isa.getHostName()); //localhost
System.out.println(isa.getAddress()); //localhost/127.0.0.1
System.out.println(isa.getPort()); //80
InetSocketAddress isa1 = new InetSocketAddress(ia4, 80);
System.out.println(isa1.getHostName()); //www.baidu.com
System.out.println(isa1.getAddress()); //www.baidu.com/180.97.33.107
System.out.println(isa1.getPort()); //80
'```
## URL
```java
URL url = new URL("https://www.baidu.com/s?tn=02049043_8_pg&ch=3&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=activate.navicat.com");
System.out.println(url.getProtocol()); //https
System.out.println(url.getHost()); //www.baidu.com
System.out.println(url.getPort()); //-1
System.out.println(url.getFile()); ///s?tn=02049043_8_pg&ch=3&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=activate.navicat.com
System.out.println(url.getPath()); // /s
System.out.println(url.getDefaultPort()); //443
URL url1 = new URL("https://www.baidu.com");
InputStream is = url1.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("index.html"),"utf-8"));
String lineString=null;
while((lineString=br.readLine())!=null) {
bw.write(lineString);
bw.newLine();
}
bw.close();
br.close();
URL url = new URL("http://www.baidu.com/index.html#aa?uname=bjsxt");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getFile());
System.out.println(url.getPath());
System.out.println(url.getRef()); // 锚点,参数一起返回
System.out.println(url.getQuery()); //存在锚点,返回null
URL url1 = new URL("http://www.baidu.com");
InputStream iStream = url.openConnection().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream,"utf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\baidu.html"),"utf-8"));
String res = null;
while((res=br.readLine())!=null) {
bw.append(res);
bw.newLine();
}
bw.flush();
bw.close();
br.close();
// byte[] buf = new byte[1024];
// int len=0;
// while((len=iStream.read(buf))!=-1) {
// System.out.println(new String(buf,0,len));
// }
// iStream.close();