Java.net包
Java.net包下常用的功能大致分為如下幾個部分:

地址
地址(IP)是指主機地址或者用作主機的標(biāo)識符或者用作套接字端點標(biāo)識符。
例如:主機的IP地址為123.123.123.123,主機的Host為“COM12345”。
地址最常用的類是InetAddress,它表示Internet協(xié)議下IP地址。它的用法如下:
// 獲取本機的IP地址和主機名
try {
InetAddress address = InetAddress.getLocalHost();
System.out.println("IP地址: " + address.getHostAddress());
System.out.println("主機名 : " + address.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
根據(jù)IP地址或者主機名獲得InetAddress:
try {
InetAddress address = InetAddress.getByName("123.123.123.123");
System.out.println("IP地址: " + address.getHostAddress());
System.out.println("主機名 : " + address.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
套接字
在客戶機/服務(wù)器工作模式中,在Server端,要準備接受多個Client端計算機的通信。為此,除用IP地址標(biāo)識Internet上的計算機之外,另還引入端口號,用端口號標(biāo)識正在Server端后臺服務(wù)的線程。端口號與IP地址的組合稱為網(wǎng)絡(luò)套接字(socket)。
java.net 包提供 4 種套接字:
-
Socket: 是 TCP 客戶端 API,通常用于連接遠程主機。 -
ServerSocket: 是 TCP 服務(wù)器 API,通常接受源于客戶端套接字的連接。 -
DatagramSocket: 是 UDP 端點 API,用于發(fā)送和接收數(shù)據(jù)包 -
MulticastSocket:是 DatagramSocket 的子類,在處理多播組時使用。
使用 TCP 套接字的發(fā)送和接收操作需要借助 InputStream 和 OutputStream 來完成,這兩者是通過Socket.getInputStream() 和 Socket.getOutputStream() 方法獲取的。
實例會在后面介紹TCP和UDP的時候介紹。
網(wǎng)絡(luò)接口
網(wǎng)絡(luò)接口在java.net包中特指NetworkInterface類,它提供 API 以瀏覽和查詢本地機器的所有網(wǎng)絡(luò)接口(例如,以太網(wǎng)連接或 PPP 端點)。只有通過該類才可以檢查是否將所有本地接口都配置為支持 IPv6。
一般用它來獲取某一個網(wǎng)卡的信息,或者本機所有網(wǎng)卡的信息,用法如下:
try {
// 通過真實的網(wǎng)卡名獲取接口
NetworkInterface ni = NetworkInterface.getByName("eth4");
// 通過InetAddress獲取接口
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni1 = NetworkInterface.getByInetAddress(address);
// 獲取本機所有網(wǎng)絡(luò)接口
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
// ...
}
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
資源標(biāo)識符/定位符
URI: 全稱統(tǒng)一資源標(biāo)識符,它是一種采用特定的語法標(biāo)識一個資源的字符串表示,簡記為它是標(biāo)識一個資源的字符串。
它的格式為:
//模式:模式特定部分
scheme:scheme specific part
模式一般格式有:
-
data:: 鏈接中直接包含經(jīng)過BASE64編碼的數(shù)據(jù) -
file::本地磁盤上的文件 -
ftp::FTP服務(wù)器 -
http::使用超文本傳輸協(xié)議 -
mailto::電子郵件的地址
模式特定部分沒有特別的要求,但一般都遵守同一種結(jié)構(gòu)形式:
//授權(quán)機構(gòu)/路徑?查詢參數(shù)
//authority/path?query
例如:
模式://授權(quán)機構(gòu)/路徑?查詢參數(shù)
http://www.baidu.com/s?ie=utf-8
URI獲取各個模式部分:
URI uri = URI.create("http://www.baidu.com/s?ie=UTF-8");
System.out.println(uri.getScheme()); // http
System.out.println(uri.getAuthority()); // www.baidu.com
System.out.println(uri.getHost()); // www.baidu.com
System.out.println(uri.getPath()); // /s
System.out.println(uri.getPort()); // -1
System.out.println(uri.getRawQuery()); // ie=UTF-8
解析URI:
URI uri1 = URI.create("http://www.baidu.com:8080/abc.html");
URI uri2 = URI.create("/replace.html");
// 替換URL路徑
URI uri = uri1.resolve(uri2);
System.out.println(uri); // http://www.baidu.com:8080/replace.html
URI uri3 = URI.create("http://www.baidu.com:8080/");
// 解析出相對路徑
URI uriNew = uri3.relativize(uri1);
System.out.println(uriNew); // abc.html
URL:也就是統(tǒng)一資源位置。實際上,URL就是一種特殊的URI,它除了標(biāo)識一個資源,還會為資源提供一個特定的網(wǎng)絡(luò)位置,客戶端可以通過它來獲取URL對應(yīng)的資源。
它的格式為:
protocol://userInfo@host:port/path?query#fragment
協(xié)議://用戶信息@主機名:端口/路徑?查詢#片段
創(chuàng)建URL的幾種方式:
URL url = new URL("http", "192.168.123.123", 8080, "/index");
System.out.println(url.toString()); // http://192.168.123.123:8080/index
URL url1 = new URL("http://192.168.123.123:8080/index");
System.out.println(url1.toString()); // http://192.168.123.123:8080/index
URL url2 = new URL("http", "192.168.123.123", "/index");
System.out.println(url2.toString()); // http://192.168.123.123/index
URL context = new URL("http", "192.168.123.123", "/index");
URL url3 = new URL(context, "/login");
System.out.println(url3.toString()); // http://192.168.123.123/login
URL中常用的方法:
// 輸出網(wǎng)絡(luò)地址的內(nèi)容
URL url = new URL("https://www.baidu.com");
InputStream stream = url.openStream();
byte[] bytes = new byte[1024];
while (stream.read(bytes, 0, bytes.length) != -1) {
String str = new String(bytes);
System.out.println(str);
}
stream.close();
//獲取模式(協(xié)議)
url.getProtocol()
//獲取主機名
url.getHost()
//獲取授權(quán)機構(gòu),一般是host:port的形式
url.getAuthority()
//獲取端口號port
url.getPort()
//返回協(xié)議的默認端口,如http協(xié)議的默認端口號為80,如果沒有指定協(xié)議的默認端口則返回-1
url.getDefaultPort()
//返回URL字符串中從主機名后的第一個斜桿/一直到片段標(biāo)識符的#字符之前的所有字符
//https://localhost:8080/search?name=doge#anchor-1
url.getFile() // /search?name=doge
//返回的值和getFile()相似,但是不包含查詢字符串
//https://localhost:8080/search?name=doge#anchor-1
url.getPath() // /search
//返回URL的片段標(biāo)識符部分
url.getRef()
//返回URL的查詢字符串
url.getQuery()
//返回URL中的用戶信息,不常用
url.getUserInfo()
URL的編碼:URL出現(xiàn)的時候,Unicode沒有普及,因此當(dāng)時規(guī)定字符必須是ASCII中的子集。所以,其他字符要使用時,必須經(jīng)過編碼轉(zhuǎn)換成ASCII碼后才能識別。
String baseUrl = "http://localhost:9090";
String path = "/index?name=派大星doge";
String encode = URLEncoder.encode(path, "UTF-8");
System.out.println(baseUrl + encode); // http://localhost:9090%2Findex%3Fname%3D%E6%B4%BE%E5%A4%A7%E6%98%9Fdoge
String decode = URLDecoder.decode(encode, "UTF-8");
System.out.println(baseUrl + decode); // http://localhost:9090/index?name=派大星doge