java获取当前电脑的ip_使用Java获取当前计算机的IP地址
問題
我正在嘗試開發一個系統,其中有不同的節點在不同的系統上或在同一系統上的不同端口上運行。
現在所有節點都創建一個Socket,其目標IP作為稱為自舉節點的特殊節點的IP。然后節點創建自己的ServerSocket并開始偵聽連接。
引導節點維護一個節點列表,并在被查詢時返回它們。
現在我需要的是節點必須將其IP注冊到自舉節點。我嘗試使用cli.getInetAddress(),因為客戶端連接到引導節點的ServerSocket,但這不起作用。
我需要客戶端注冊其PPP IP(如果可用);
否則LAN IP(如果有);
否則它必須注冊127.0.0.1,假設它是同一臺計算機。
使用代碼:
System.out.println(Inet4Address.getLocalHost().getHostAddress());
要么
System.out.println(InetAddress.getLocalHost().getHostAddress());
我的PPP連接IP地址是:117.204.44.192,但上面的返回值為192.168.1.2
編輯
我使用以下代碼:
Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface) e.nextElement();
Enumeration ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress) ee.nextElement();
System.out.println(i.getHostAddress());
}
}
我能夠獲得所有與NetworkInterface相關的IP地址,但我如何區分它們呢?這是我得到的輸出:
127.0.0.1
192.168.1.2
192.168.56.1
117.204.44.19
#1 熱門回答(233 贊)
在最一般的情況下,這可能有點棘手。
從表面上看,InetAddress.getLocalHost()應該為你提供該主機的IP地址。問題是主機可能有很多網絡接口,接口可能綁定到多個IP地址。最重要的是,并非所有IP地址都可以在你的計算機或LAN之外訪問。例如,它們可以是虛擬網絡設備的IP地址,專用網絡IP地址等。
這意味著InetAddress.getLocalHost()返回的IP地址可能不適合使用。
你怎么處理這個?
一種方法是使用NetworkInterface.getNetworkInterfaces()獲取主機上的所有已知網絡接口,然后迭代每個NI的地址。
另一種方法是(以某種方式)獲取主機的外部廣告FQDN,并使用InetAddress.getByName()來查找主IP地址。 (但是你如何得到它,以及如何處理基于DNS的負載均衡器?)
前一個版本的變體是從配置文件或命令行參數中獲取首選FQDN。
另一種變體是從配置文件或命令行參數中獲取首選IP地址。
總之,InetAddress.getLocalHost()通常可以工作,但是你可能需要為在"復雜"網絡環境中運行代碼的情況提供替代方法。
我能夠獲得與所有網絡接口相關的所有IP地址,但我如何區分它們?
127.xxx.xxx.xxx范圍內的任何地址都是"環回"地址。它只對"這個"主機可見。
192.168.xxx.xxx范圍內的任何地址都是私有(即本地站點)IP地址。這些保留在組織內使用。這同樣適用于10.xxx.xxx.xxx地址,172.16.xxx.xxx到172.31.xxx.xxx。
169.254.xxx.xxx范圍內的地址是鏈路本地IP地址。這些保留用于單個網段。
224.xxx.xxx.xxx到239.xxx.xxx.xxx范圍內的地址是多播地址。
地址255.255.255.255是廣播地址。
其他任何東西都應該是有效的公共點對點IPv4地址。
事實上,InetAddress API提供了測試環回,鏈接本地,站點本地,多播和廣播地址的方法。你可以使用這些來排序你獲得的哪個IP地址是最合適的。
#2 熱門回答(49 贊)
這里發布的測試IP歧義解決方法代碼來自https://issues.apache.org/jira/browse/JCS-40(InetAddress.getLocalHost()在Linux系統上不明確):
/**
* Returns an InetAddress object encapsulating what is most likely the machine's LAN IP address.
*
* This method is intended for use as a replacement of JDK method InetAddress.getLocalHost, because
* that method is ambiguous on Linux systems. Linux systems enumerate the loopback network interface the same
* way as regular LAN network interfaces, but the JDK InetAddress.getLocalHost method does not
* specify the algorithm used to select the address returned under such circumstances, and will often return the
* loopback address, which is not valid for network communication. Details
* here.
*
* This method will scan all IP addresses on all network interfaces on the host machine to determine the IP address
* most likely to be the machine's LAN address. If the machine has multiple IP addresses, this method will prefer
* a site-local IP address (e.g. 192.168.x.x or 10.10.x.x, usually IPv4) if the machine has one (and will return the
* first site-local address if the machine has more than one), but if the machine does not hold a site-local
* address, this method will return simply the first non-loopback address found (IPv4 or IPv6).
*
* If this method cannot find a non-loopback address using this selection algorithm, it will fall back to
* calling and returning the result of JDK method InetAddress.getLocalHost.
*
*
* @throws UnknownHostException If the LAN address of the machine cannot be found.
*/
private static InetAddress getLocalHostLANAddress() throws UnknownHostException {
try {
InetAddress candidateAddress = null;
// Iterate all NICs (network interface cards)...
for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = (NetworkInterface) ifaces.nextElement();
// Iterate all IP addresses assigned to each card...
for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
InetAddress inetAddr = (InetAddress) inetAddrs.nextElement();
if (!inetAddr.isLoopbackAddress()) {
if (inetAddr.isSiteLocalAddress()) {
// Found non-loopback site-local address. Return it immediately...
return inetAddr;
}
else if (candidateAddress == null) {
// Found non-loopback address, but not necessarily site-local.
// Store it as a candidate to be returned if site-local address is not subsequently found...
candidateAddress = inetAddr;
// Note that we don't repeatedly assign non-loopback non-site-local addresses as candidates,
// only the first. For subsequent iterations, candidate will be non-null.
}
}
}
}
if (candidateAddress != null) {
// We did not find a site-local address, but we found some other non-loopback address.
// Server might have a non-site-local address assigned to its NIC (or it might be running
// IPv6 which deprecates the "site-local" concept).
// Return this non-loopback candidate address...
return candidateAddress;
}
// At this point, we did not find a non-loopback address.
// Fall back to returning whatever InetAddress.getLocalHost() returns...
InetAddress jdkSuppliedAddress = InetAddress.getLocalHost();
if (jdkSuppliedAddress == null) {
throw new UnknownHostException("The JDK InetAddress.getLocalHost() method unexpectedly returned null.");
}
return jdkSuppliedAddress;
}
catch (Exception e) {
UnknownHostException unknownHostException = new UnknownHostException("Failed to determine LAN address: " + e);
unknownHostException.initCause(e);
throw unknownHostException;
}
}
#3 熱門回答(40 贊)
你可以使用java'sInetAddressclass來實現此目的。
InetAddress IP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+IP.getHostAddress());
我系統的輸出= IP of my system is := 10.100.98.228
返回文本演示文稿中的IP地址字符串。
或者你也可以
InetAddress IP=InetAddress.getLocalHost();
System.out.println(IP.toString());
輸出= IP of my system is := RanRag-PC/10.100.98.228
總結
以上是生活随笔為你收集整理的java获取当前电脑的ip_使用Java获取当前计算机的IP地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件工程课的分数系统,和打分方法
- 下一篇: 中大型计算机代表型号,目前个人计算机主要