0

Here is my ipconfig stat

Ethernet adapter Ethernet 3:

Connection-specific DNS Suffix  . :
IPv6 Address. . . . . . . . . . . : 
**IPv4 Address. . . . . . . . . . . :** -> This I want to retrieve.
Subnet Mask . . . . . . . . . . . : 
Default Gateway . . . . . . . . . : ::
                                   

Ethernet adapter Ethernet:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Unknown adapter Local Area Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 1:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 3:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

Connection-specific DNS Suffix  . :
IPv6 Address. . . . . . . . . . . : 
Temporary IPv6 Address. . . . . . : 
Temporary IPv6 Address. . . . . . : 
Temporary IPv6 Address. . . . . . : 
Temporary IPv6 Address. . . . . . : 
Temporary IPv6 Address. . . . . . : 
Temporary IPv6 Address. . . . . . : 
Link-local IPv6 Address . . . . . : 
IPv4 Address. . . . . . . . . . . : 
Subnet Mask . . . . . . . . . . . : 
Default Gateway . . . . . . . . . : 
                                  

Ethernet adapter Bluetooth Network Connection:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Ethernet adapter vEthernet (Default Switch):

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : 
IPv4 Address. . . . . . . . . . . : 
Subnet Mask . . . . . . . . . . . : 
Default Gateway . . . . . . . . . :

Here is the code snippet I used

import java.net.InetAddress;
 
class IPAddressExample {

        public static void main(String args[]) throws Exception {
        InetAddress inetAddress = InetAddress.getLocalHost();
        System.out.println("IP Address:- " + inetAddress.getHostAddress());
        System.out.println("Host Name:- " + inetAddress.getHostName());
    }
}

This gives me 127.0.0.1, but I need the local ip(the one which I have made bold). Tried many solutions that is there online, but all gave 127.0.0.1, instead of 10.xx.xxx.xxx

Any solution for this

Marc
  • 2,738
  • 1
  • 17
  • 21
Salman Khurshid
  • 101
  • 1
  • 2
  • 8
  • 2
    Does this answer your question? [Getting the IP address of the current machine using Java](https://stackoverflow.com/questions/9481865/getting-the-ip-address-of-the-current-machine-using-java) – lakshman Aug 31 '20 at 10:13
  • @Attilajáger, OP is looking for code that gets the local IP, but the answer is for remote IP. – Deadbeef Aug 31 '20 at 10:16

1 Answers1

0

Use the code below to iterate over IP addresses for every interface.

Code is from this question.

        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        while(e.hasMoreElements())
        {
            NetworkInterface n = e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements())
            {
                InetAddress i = ee.nextElement();
                // System.out.println(i.getHostAddress());
                // Do stuff here
            }
        }
Deadbeef
  • 1,499
  • 8
  • 20