3

hi i am using the following code to get the local machine ip from java applet but i always getting 127.0.0.1 instead of actual ip

public String ip;
public void init()
{
    try
    {
        Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
        for (; n.hasMoreElements();)
        {
            NetworkInterface e = n.nextElement();

            Enumeration<InetAddress> a = e.getInetAddresses();
            for (; a.hasMoreElements();)
            {
                InetAddress addr = a.nextElement();
                ip = "Really " + addr.getHostAddress();
                System.out.println(ip);
            }
        } 
    }
    catch(Exception ex)
    {
    }       
}
Seagull
  • 13,484
  • 2
  • 33
  • 45
user3373819
  • 89
  • 2
  • 10

4 Answers4

1

Try this sample:

import java.net.InetAddress;
...

ip = InetAddress.getLocalHost();
System.out.println("Current IP address : " + ip.getHostAddress());

I've found this solution here: How to get Server IP address in Java and it works for my.

Tomasz Dzięcielewski
  • 3,829
  • 4
  • 33
  • 47
AppLend
  • 1,644
  • 1
  • 15
  • 12
  • 1
    If there is IPv6 or multiple interfaces that would not be adequate, but +1 because maybe that's all the OP needed. – Jason C Mar 03 '14 at 07:13
  • 1
    when i run your code inside eclipse getting exact ip, but when i run the applet from html page i am getting same 127.0.0.1 – user3373819 Mar 03 '14 at 07:24
  • Occasionally found old topic about the same problem: https://bugzilla.mozilla.org/show_bug.cgi?id=58391 – AppLend Mar 03 '14 at 07:32
  • 1
    looks like we can not get actual ip from java applet because of some permission issues – user3373819 Mar 03 '14 at 08:14
0

You cannot get it this info with an unsigned applet, although you may be able to grant your applet the appropriate policy. First read this for general instructions on setting up policy files. You will need to grant it the "getNetworkInformation" permission. You may need to sign it.

Once you've got that taken care of, you can use NetworkInterface to get interface information. Only examine active interfaces, and skip loopback and link-local addresses, e.g.:

Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
while (ifaces.hasMoreElements()) {
    NetworkInterface iface = ifaces.nextElement();
    if (iface.isUp()) {
        Enumeration<InetAddress> addrs = iface.getInetAddresses();
        while (addrs.hasMoreElements()) {
            InetAddress addr = addrs.nextElement();
            if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress())
                System.out.println(addr.getHostAddress());
        }
    }
}

Keep in mind that if multiple interfaces are present, there may be multiple IPs. Also you will get both IPv4 and IPv6 addresses. You can filter protocols by checking to see if the InetAddress is an instance of Inet4Address or Inet6Address, e.g.:

if (!addr.isLinkLocalAddress() && !addr.isLoopbackAddress() && (addr instanceof Inet4Address))
    ...; // IPv4 addresses only.
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • when i run your code inside eclipse getting exact ip, but when i run the applet from html page i am getting null – user3373819 Mar 03 '14 at 07:24
  • 1
    i am edited your code System.out.println(addr.getHostAddress()); now i am getting both ipv4 and ipv6 addresses , i need ipv4 address only – user3373819 Mar 03 '14 at 07:29
  • @user3373819 You can check if `addr instanceof Inet4Address` to see if it's IPv4. See inheritance diagram for [`InetAddress`](http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html). – Jason C Mar 04 '14 at 21:11
  • @user3373819 Also see my edit to this answer regarding security. – Jason C Mar 04 '14 at 21:24
  • if run the applet from another machine i am getting null – user3373819 Mar 06 '14 at 01:35
  • @user3373819 Did you set up your policy file correctly as described? – Jason C Mar 06 '14 at 01:39
  • are you sure if i sign the applet will i get the exact IP? – user3373819 Mar 06 '14 at 01:48
  • hi jason can you guide me how to set the policy file? – user3373819 Mar 06 '14 at 01:52
  • @user3373819 No. You have to add the getNetworkInformation permission to the policy file. You have to sign it to allow it to run on other users' systems, but for development you can modify your default unsigned applet policy file (see http://stackoverflow.com/questions/19424410/changing-java-policy-file-for-applet). – Jason C Mar 06 '14 at 01:53
  • @user3373819 The linked documentation and questions have all the information you will need to be able to set up your policy file, and it is a good skill to learn. If you have specific questions about your policy file please open a new question. – Jason C Mar 06 '14 at 01:55
0

may be script solution more acceptable to you?

Like this:

<script>
var myip;
function ip_callback(o) {
    ip = o.host;
}
</script>
<script src="https://smart-ip.net/geoip-json?callback=ip_callback"></script>
<script>alert(ip);</script>

In this case this topic will be useful: Get client IP address via third party web service

Community
  • 1
  • 1
AppLend
  • 1,644
  • 1
  • 15
  • 12
0

You can check if the network address is an instanceof Inet4Address. If it returns true, you can pick the same.

Example 1:

InetAddress ia = InetAddress.getLocalHost();
System.out.println( "ia.getLocalHost(): " + ia );
System.out.println( "ia.getHostAddress(): " + ia.getHostAddress() );

// true for 192.168.1.193 or 127.0.0.1
boolean isIpv4 = ( ia instanceof Inet4Address );

// true for 2001:0:9d38:6abd:186b:38da:3f57:fe3e
boolean isIpv6 = ( ia instanceof Inet6Address );

In the case of getHostAddress() returning 127.0.0.1, you better go with NetworkInterface to identify local IP addresses from which you can find the IP4 address if a network interface is up.

Example 2:

Enumeration<NetworkInterface> enumNwi = NetworkInterface.getNetworkInterfaces();
while( enumNwi.hasMoreElements() ) {
    NetworkInterface nwi = enumNwi.nextElement();
    if ( nwi.isUp() ) { // if the nw is up and running
        Enumeration<InetAddress> enumInetAddresses = nwi.getInetAddresses();
        while ( enumInetAddresses.hasMoreElements() ) {
            InetAddress inetAddress = enumInetAddresses.nextElement();
            // check if it is not 127.0.0.1
            if ( ! inetAddress.isLinkLocalAddress() &&
                 ! inetAddress.isLoopbackAddress() ) {
                System.out.println( inetAddress.getHostAddress() + 
                                    ( inetAddress instanceof Inet4Address ?
                                      " <<------ IP4 address" : "" ) );
            }
        }
    }
}

Refer to:

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • 1
    same..., if i run the code inside eclipse i am getting result, but when i call from html page i am getting null. i think this is the security issue. applet will not allow to get the actual IP. – user3373819 Mar 04 '14 at 02:30