Java getting my IP address

前端 未结 10 2361
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 16:53

I am trying to get my Internet IP address in Java but I keep getting my local address (ie: 127.0.0.1), when my IP address is 192.168.0.xxx

I am using the line:

相关标签:
10条回答
  • 2020-11-27 17:43

    Here is my approach to get the IP address.

    1. Get your default gateway address
    2. Get all the address in your PC
    3. Now in your IP(suppose 192.168.100.4) and default gateway IP(suppose 192.168.100.1) have first 9 digit of address must be same, so whichever IP who full this criteria is your IP.

    Please see below for full working code.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.util.Enumeration;
    import java.util.Iterator;
    import java.util.StringTokenizer;
    import java.util.TreeSet;
    
    public class MyIpAddress {
    
        public static void main(String[] args) {
            // doPortForwarding();
    
            MyIpAddress myIpAddress = new MyIpAddress();
    
            // get default address
            String yourIp = myIpAddress.getYourIp(myIpAddress
                    .getDefaultGateWayAddress());
            System.out.println(yourIp);
    
            // get
    
        } // amin
    
        // return ip address for which u need to do port forwarding
        private String getYourIp(String defaultAddress) {
    
            String temp = defaultAddress.substring(0, 11);
            String ipToForward = "";
    
            TreeSet<String> ipAddrs = getIpAddressList();
            for (Iterator<String> iterator = ipAddrs.iterator(); iterator.hasNext();) {
    
                String tempIp = iterator.next();
                if (tempIp.contains(temp)) {
                    ipToForward = tempIp;
                    break;
                }
            }
    
            return ipToForward;
    
        }// ipForPortForwarding
    
        // get the ipaddress list
        private TreeSet<String> getIpAddressList() {
            TreeSet<String> ipAddrs = new TreeSet<String>();
    
            try {
                Enumeration<NetworkInterface> interfaces = NetworkInterface
                        .getNetworkInterfaces();
                while (interfaces.hasMoreElements()) {
                    NetworkInterface iface = interfaces.nextElement();
                    // filters out 127.0.0.1 and inactive interfaces
                    if (iface.isLoopback() || !iface.isUp())
                        continue;
    
                    Enumeration<InetAddress> addresses = iface.getInetAddresses();
                    while (addresses.hasMoreElements()) {
    
                        InetAddress addr = addresses.nextElement();
    
                        ipAddrs.add(addr.getHostAddress());
    
                    }// 2 nd while
                }// 1 st while
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return ipAddrs;
    
        }// getIpAddressList
    
        // get default gateway address in java
    
        private String getDefaultGateWayAddress() {
            String defaultAddress = "";
            try {
                Process result = Runtime.getRuntime().exec("netstat -rn");
    
                BufferedReader output = new BufferedReader(new InputStreamReader(
                        result.getInputStream()));
    
                String line = output.readLine();
                while (line != null) {
                    if (line.contains("0.0.0.0")) {
    
                        StringTokenizer stringTokenizer = new StringTokenizer(line);
                        stringTokenizer.nextElement();// first string is 0.0.0.0
                        stringTokenizer.nextElement();// second string is 0.0.0.0
                        defaultAddress = (String) stringTokenizer.nextElement(); // this is our default address
                        break;
                    }
    
                    line = output.readLine();
    
                }// while
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return defaultAddress;
    
        }// getDefaultAddress
    }
    
    0 讨论(0)
  • 2020-11-27 17:45

    You can get your IP address by writing a simple code. `

    import java.net.InetAddress;
    
    public class Main {
        public static void main(String[] args) throws Exception
        {
            System.out.println(InetAddress.getLocalHost());
        }
    }
    
    0 讨论(0)
  • 2020-11-27 17:48

    The NetworkInterface class contains all the relevant methods, but be aware that there's no such thing as "my IP". A machine can have multiple interfaces and each interface can have multiple IPs.

    You can list them all with this class but which interface and IP you choose from the list depends on what you exactly need to use this IP for.

    (InetAddress.getLocalHost() doesn't consult your interfaces, it simply returns constant 127.0.0.1 (for IPv4))

    0 讨论(0)
  • 2020-11-27 17:48
        String ip;
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                // filters out 127.0.0.1 and inactive interfaces
                if (iface.isLoopback() || !iface.isUp())
                    continue;
    
                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while(addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    ip = addr.getHostAddress();
                    System.out.println(iface.getDisplayName() + " " + ip);
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
    
    0 讨论(0)
提交回复
热议问题