W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
InetAddress測試: 解決ip地址問題。
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Test {
public static void main(String[] args) throws UnknownHostException {
/**
* InetAddress沒有封裝端口
*/
//使用getLocalHost方法創(chuàng)建InetAddress對象
InetAddress address = InetAddress.getLocalHost();
System.out.println(address.getHostAddress());//返回本機的ip地址
System.out.println(address.getHostName());//輸出本機計算機名
//根據(jù)域名得到InetAddress對象
address = InetAddress.getByName("www.163.com");
System.out.println(address.getHostAddress());//輸出163服務(wù)器的ip:125.39.21.3
System.out.println(address.getHostName());//輸出域名:www.163.com
//根據(jù)ip得到InetAddress對象
address = InetAddress.getByName("125.39.21.3");
System.out.println(address.getHostAddress());//輸出163服務(wù)器的ip:125.39.21.3
System.out.println(address.getHostName());//如果ip被解析(DNS)則輸出域名,如果沒被解析則輸出ip。
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: