溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java 網(wǎng)絡(luò)編程-得到ip對(duì)象InetAddress的三種方式

發(fā)布時(shí)間:2020-08-01 15:39:12 來源:網(wǎng)絡(luò) 閱讀:564 作者:wx5d21d5e6e5ab1 欄目:編程語言

網(wǎng)絡(luò)編程:跟服務(wù)器底層源碼有關(guān)
B/S在公網(wǎng)上(瀏覽器訪問)服務(wù)器端和客戶端只寫一端,c/s在局域網(wǎng)上(網(wǎng)吧)服務(wù)器端和客戶端都要寫,B/S是大大的C/S
協(xié)議:端口之間的交流更暢通,不同軟件有各自的端口
tcp/udp/http

ip:定位一個(gè)節(jié)點(diǎn)
使用靜態(tài)方法getLocalHost方法創(chuàng)建InetAddress對(duì)象,InetAddress沒有構(gòu)造器
InetAddress addr = InetAddress.getLocalHost();
addr.getHostAddress() 本機(jī)地址
addr.getHostName() 計(jì)算機(jī)名

 public class http {

public static void main(String[]args) throws UnknownHostException
{
    //使用靜態(tài)方法getLocalHost方法創(chuàng)建InetAddress對(duì)象
    InetAddress addr = InetAddress.getLocalHost();

    System.out.println(addr.getHostAddress());
    System.out.println(addr.getHostName());

    //使用域名得到InetAddress對(duì)象
    addr=InetAddress.getByName("www.163.com");
    System.out.println(addr.getHostAddress());//返回163服務(wù)器的ip
    System.out.println(addr.getHostName());//輸出服務(wù)器名

    //根據(jù)ip地址得到InetAddress對(duì)象
    addr=InetAddress.getByName("118.118.221.77");
    System.out.println(addr.getHostAddress());
    System.out.println(addr.getHostName());//若ip地址不存在或者DNS不允許,則返回ip地址而不是域名
}

}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI