JAVA_網路Module_課堂
切記要引入 java.net 庫
import java.net.*;
InetAddress 類別
包含 兩個欄位
(1)主機名稱
透過 InetAddress類別 提供的getHostName()方法
取得本機名稱
(2)IP位址
透過 InetAddress類別 提供的getHostAddress()方法
取得本機IP
--------------------------------------------------------------------
import java.net.*;
public class javaNet_p1 {
public static void main(String[] args)
{
InetAddress localIP;
//InetAddress 類別
try
{
localIP = InetAddress.getLocalHost();
String ip = localIP.getHostAddress();
String HostName = localIP.getHostName();
//String ip = localIP.getHostAddress();
System.out.println("\n");
System.out.println("name and ip is " + localIP);
System.out.println("\n");
System.out.println("ip is "+ip);
System.out.println("\n");
System.out.println("HostName is "+ HostName);
System.out.println("\n");
System.out.println("\n\n");
//System.out.println("name and ip is " + localIP);
}
catch( UnknownHostException e){}
}
}
--------------------------------------------------------------------
在訪問某些域名時
getCanonicalHostName 方法和
getHostName 方法
返回值是一樣的
DNS 服務器如何解釋主機名和主機別名
以及它們的設置有關。
目前這是本台電腦的 IP 位址 對應顯示本台電腦名稱
下面我們用這個網址來做比較比較清晰
www.yahoo.com
InetAddress類別 : 不以建構子方式產生物件,而是使用「靜態方法」
Static InetAddress getByName(String host)

Static InetAddress[] getAllByName(String host)

NetworkInterface類別 : 列出主機上所有網路介面判別是否以啟用(return bool)
Static Enumeration<NetworkInterface>getNetworkInterfaces()
Static NetworkInterface getByName(String name)

URI類別



留言
張貼留言