Java getting my IP address

前端 未结 10 2406
没有蜡笔的小新
没有蜡笔的小新 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 ipAddrs = getIpAddressList();
            for (Iterator 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 getIpAddressList() {
            TreeSet ipAddrs = new TreeSet();
    
            try {
                Enumeration 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 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
    }
    

提交回复
热议问题