国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java網(wǎng)絡(luò)編程InetAddress和InetSocketAddress

2022-09-03 09:12 作者:虛云幻仙  | 我要投稿

/**
* java網(wǎng)絡(luò)編程
* 測試InetAddress類及InetSocketAddress類
* java中網(wǎng)絡(luò)應(yīng)用通信需要通過java.net包提供的網(wǎng)絡(luò)功能間接調(diào)用操作系統(tǒng)接口
* InetAddress封裝主機(jī)的ip地址和域名
*/

public class TestInetAddress {
? ?public static void main(String[] args) {
? ? ? ?try {
? ? ? ? ? ?InetAddress host = InetAddress.getLocalHost();
? ? ? ? ? ?//InetAddress類構(gòu)造器私有,需要通過靜態(tài)方法得到InetAddress對象
? ? ? ? ? ?//.getLocalHost()得到本地主機(jī)的InetAddress對象

? ? ? ? ? ?System.out.println(host.getHostAddress());
? ? ? ? ? ?//.getHostAddress()返回主機(jī)的ip地址字符串,結(jié)果為192.168.xxx.xxx私有/C類地址,非注冊地址,內(nèi)部使用。
? ? ? ? ? ?// 環(huán)回地址為127.xxx.xxx.xxx用于主機(jī)向自己發(fā)送通信,常用本機(jī)地址為127.0.0.1
? ? ? ? ? ?//目標(biāo)地址為環(huán)回地址的數(shù)據(jù)在向下傳遞到網(wǎng)絡(luò)層/IP層時不再向下傳遞,轉(zhuǎn)發(fā)給本機(jī)IP層處理向上傳遞,繞開TCP/IP協(xié)議棧的下層,不會通過網(wǎng)卡發(fā)送出去

? ? ? ? ? ?System.out.println(host.getHostName());
? ? ? ? ? ?//.getHostName()返回主機(jī)名字符串,結(jié)果為LAPTOP-xxxxxx

? ? ? ? ? ?InetAddress baidu = InetAddress.getByName("www.baidu.com");
? ? ? ? ? ?//.getByName(String host)通過域名獲取InetAddress對象,方法內(nèi)部會進(jìn)行域名解析
? ? ? ? ? ?System.out.println(baidu.getHostAddress());
? ? ? ? ? ?//返回百度網(wǎng)的ip地址39.156.66.18,ipv4地址為32位bit/4字節(jié)byte,字節(jié)之間用.分開
? ? ? ? ? ?System.out.println(baidu.getHostName());
? ? ? ? ? ?//.getHostName()返回hostName屬性的值,如果在實例化InetAddress對象時使用String域名作為參數(shù),則getHostName()返回該域名
? ? ? ? ? ?byte[] baiduIP = baidu.getAddress();
? ? ? ? ? ?//.getAddress()返回ip地址的字節(jié)數(shù)組byte[],39.156.66.18轉(zhuǎn)換為[39, -100, 66, 18],byte從-128到127,156溢出-256=-100
? ? ? ? ? ?System.out.println(Arrays.toString(baiduIP));

? ? ? ? ? ?InetAddress baidu1 = InetAddress.getByAddress(baiduIP);
? ? ? ? ? ?//使用byte[]數(shù)組實例化InetAddress
? ? ? ? ? ?System.out.println(baidu1.getHostName());
? ? ? ? ? ?//通過ip地址實例化的對象.getHostName(),由于沒有通過安全管理器security manager的安全檢查security check,結(jié)果返回了ip地址39.156.66.14

? ? ? ? ? ?System.out.println(InetAddress.getByName("39.156.66.14").getHostName());
? ? ? ? ? ?//通過ip地址字符串實例化對象,getHostName()同樣返回了ip地址
? ? ? ?} catch (UnknownHostException e) {
? ? ? ? ? ?throw new RuntimeException(e);
? ? ? ?}

? ? ? ?InetSocketAddress isa = new InetSocketAddress("www.baidu.com",80);
? ? ? ?//InetSocketAddress類與InetAddress類用法相近,可以用new實例對象,構(gòu)造器中除了域名/ip地址以外需要額外指定port端口號
? ? ? ?//主機(jī)的端口號為16位,對應(yīng)0-65535端口,是虛擬的概念,網(wǎng)絡(luò)程序都有自己的端口, 80端口為http默認(rèn)端口
? ? ? ?//socket套接字

? ? ? ?System.out.println(isa.getHostName());
? ? ? ?//作用同InetAddress,結(jié)果為:www.baidu.com
? ? ? ?InetAddress ia = isa.getAddress();
? ? ? ?//InetSocketAddress的.getAddress()方法返回InetAddress對象
? ? ? ?System.out.println(ia.getHostAddress());
? ?}
}

java網(wǎng)絡(luò)編程InetAddress和InetSocketAddress的評論 (共 條)

分享到微博請遵守國家法律
武隆县| 革吉县| 苏州市| 揭西县| 海口市| 珠海市| 中超| 五指山市| 招远市| 红安县| 横峰县| 无极县| 吉木萨尔县| 疏附县| 罗山县| 察哈| 潜山县| 登封市| 观塘区| 柘城县| 新晃| 紫云| 玉山县| 兖州市| 河北区| 吴忠市| 朝阳县| 如东县| 潮安县| 桂东县| 乐东| 台州市| 珠海市| 新乡市| 新乐市| 云龙县| 乌兰县| 焉耆| 东阿县| 玉环县| 紫云|