Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - I am getting wrong ip from getlocalhost(). I need to get the ip associated with my gateway in my case 192.168.0.100

I want to get the ip of my machine iwth java. The problem is that getlocalhost() gives 192.168.56.1 thats not accesible from outside the network, I want to get the ip accosiated with my gateway 192.168.0.100, I am using a second Wan router and a network switch. so I used this:

Enumeration Interfaces = NetworkInterface.getNetworkInterfaces();
        while(Interfaces.hasMoreElements())
        {
            NetworkInterface Interface = (NetworkInterface)Interfaces.nextElement();
            Enumeration Addresses = Interface.getInetAddresses();
            while(Addresses.hasMoreElements())
            {
                InetAddress Address = (InetAddress)Addresses.nextElement();
                System.out.println(Address.getHostAddress());
            }
         }

It gives this:

127.0.0.1
0:0:0:0:0:0:0:1
fe80:0:0:0:d591:6dbb:7ed6:a0e2%eth1
10.203.5.52
fe80:0:0:0:e163:c52a:da9:7447%eth2
192.168.0.100
192.168.56.1
169.254.123.121
fe80:0:0:0:7950:8041:2f4:531b%eth7

One of them is the right one but that still doesnt help because I need to isolate it and I (by I Mean the programm because I know the right one) dont know what is the right one to use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...