IP地址未在java中获取 [英] IP Address not obtained in java
问题描述
此代码用于将本地IP地址返回为192.xxx.x.xxx,但现在返回127.0.0.1。请帮助我为什么相同的代码返回不同的值。我需要在linux OS上看一些东西。
import java.util。*;
import java.lang。*;
import java.net。*;
公共类GetOwnIP
{
public static void main(String args []){
try {
InetAddress ownIP = InetAddress.getLocalHost();
System.out.println(我系统的IP是:=+ ownIP.getHostAddress());
} catch(例外e){
System.out.println(Exception caught =+ e.getMessage());
}
}
}
127.0.0.1是环回适配器 - 它是对(有点故障)问题我的IP地址是什么?的完全正确的回应。
问题是 b 编辑: 如果有安全管理器,则使用 是否可能行为的改变是由于权限的变化? 编辑:我相信 (我忘记了 这里有我的笔记本电脑现在的结果: (我不认为这会泄露任何非常敏感信息:) 如果你知道要使用哪个网络接口,调用 This code used to return my local ip address as 192.xxx.x.xxx but now it is returning 127.0.0.1 . Please help me why the same code is returning different value. Is there something that I need to watch at linux OS.
127.0.0.1 is the loopback adapter - it's a perfectly correct response to the (somewhat malfomed) question "what is my IP address?" The problem is that there are multiple correct answers to that question. EDIT: The docs for If there is a security manager, its
checkConnect method is called with the
local host name and -1 as its
arguments to see if the operation is
allowed. If the operation is not
allowed, an InetAddress representing
the loopback address is returned. Is it possible that the change in behaviour is due to a change in permissions? EDIT: I believe that (I'd forgotten just how awful the Here are the results on my laptop right now: (I don't think that's giving away any hugely sensitive information :) If you know which network interface you want to use, call 这篇关于IP地址未在java中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b getLocalHost的文档$ >说:
本地主机名调用其
checkConnect方法和-1作为其
参数,以查看操作是否允许
。如果操作不允许
,则返回表示
的InetAddress回送地址。
NetworkInterface.getNetworkInterfaces
是您需要列举所有可能性。这是一个不显示虚拟地址的示例,但适用于主接口:
import java.net。*;
import java.util。*;
公共类测试
{
public static void main(String [] args)
throws Exception //只是为了简单
{
for(Enumeration< NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces();
ifaces.hasMoreElements();)
{
NetworkInterface iface = ifaces.nextElement();
System.out.println(iface.getName()+:);
for(Enumeration< InetAddress> addresses =
iface.getInetAddresses();
addresses.hasMoreElements();)
{
InetAddress address = addresses.nextElement() ;
System.out.println(+ address);
}
}
}
}
Enumeration< T>
类型是多么可怕直接使用!)
lo:
/127.0.0.1
eth0:
/169.254.148.66
eth1:
eth2:
ppp0:
/10.54.251.111
NetworkInterface.getByName(...)
,然后查看该接口的地址(如上面的代码所示)。 / p> import java.util.*;
import java.lang.*;
import java.net.*;
public class GetOwnIP
{
public static void main(String args[]) {
try{
InetAddress ownIP=InetAddress.getLocalHost();
System.out.println("IP of my system is := "+ownIP.getHostAddress());
}catch (Exception e){
System.out.println("Exception caught ="+e.getMessage());
}
}
}
getLocalHost
say:
NetworkInterface.getNetworkInterfaces
is what you need to enumerate all the possibilities. Here's an example which doesn't show virtual addresses, but works for "main" interfaces:import java.net.*;
import java.util.*;
public class Test
{
public static void main(String[] args)
throws Exception // Just for simplicity
{
for (Enumeration<NetworkInterface> ifaces =
NetworkInterface.getNetworkInterfaces();
ifaces.hasMoreElements(); )
{
NetworkInterface iface = ifaces.nextElement();
System.out.println(iface.getName() + ":");
for (Enumeration<InetAddress> addresses =
iface.getInetAddresses();
addresses.hasMoreElements(); )
{
InetAddress address = addresses.nextElement();
System.out.println(" " + address);
}
}
}
}
Enumeration<T>
type is to work with directly!)lo:
/127.0.0.1
eth0:
/169.254.148.66
eth1:
eth2:
ppp0:
/10.54.251.111
NetworkInterface.getByName(...)
and then look at the addresses for that interface (as shown in the code above).