Getting the IP address of the current machine using Java

后端 未结 17 1770
眼角桃花
眼角桃花 2020-11-22 02:26

I am trying to develop a system where there are different nodes that are run on different system or on different ports on the same system.

Now all the nodes create

相关标签:
17条回答
  • 2020-11-22 02:57

    When you are looking for your "local" address, you should note that each machine has not only a single network interface, and each interface could has its own local address. Which means your machine is always owning several "local" addresses.

    Different "local" addresses will be automatically chosen to use when you are connecting to different endpoints. For example, when you connect to google.com, you are using an "outside" local address; but when you connect to your localhost, your local address is always localhost itself, because localhost is just a loopback.

    Below is showing how to find out your local address when you are communicating with google.com:

    Socket socket = new Socket();
    socket.connect(new InetSocketAddress("google.com", 80));
    System.out.println(socket.getLocalAddress());
    
    0 讨论(0)
  • 2020-11-22 02:57

    A rather simplistic approach that seems to be working...

    String getPublicIPv4() throws UnknownHostException, SocketException{
        Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
        String ipToReturn = null;
        while(e.hasMoreElements())
        {
            NetworkInterface n = (NetworkInterface) e.nextElement();
            Enumeration<InetAddress> ee = n.getInetAddresses();
            while (ee.hasMoreElements())
            {
                InetAddress i = (InetAddress) ee.nextElement();
                String currentAddress = i.getHostAddress();
                logger.trace("IP address "+currentAddress+ " found");
                if(!i.isSiteLocalAddress()&&!i.isLoopbackAddress() && validate(currentAddress)){
                    ipToReturn = currentAddress;    
                }else{
                    System.out.println("Address not validated as public IPv4");
                }
    
            }
        }
    
        return ipToReturn;
    }
    
    private static final Pattern IPv4RegexPattern = Pattern.compile(
            "^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
    
    public static boolean validate(final String ip) {
        return IPv4RegexPattern.matcher(ip).matches();
    }
    
    0 讨论(0)
  • 2020-11-22 02:58

    Usually when i try to find my public IP Address like cmyip.com or www.iplocation.net, i use this way:

    public static String myPublicIp() {
    
        /*nslookup myip.opendns.com resolver1.opendns.com*/
        String ipAdressDns  = "";
        try {
            String command = "nslookup myip.opendns.com resolver1.opendns.com";
            Process proc = Runtime.getRuntime().exec(command);
    
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    
            String s;
            while ((s = stdInput.readLine()) != null) {
                ipAdressDns  += s + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return ipAdressDns ;
    }
    
    0 讨论(0)
  • 2020-11-22 02:59

    EDIT 1: Updated code, since the previous link, exists no more

    import java.io.*;
    import java.net.*;
    
    public class GetMyIP {
        public static void main(String[] args) {
            URL url = null;
            BufferedReader in = null;
            String ipAddress = "";
            try {
                url = new URL("http://bot.whatismyipaddress.com");
                in = new BufferedReader(new InputStreamReader(url.openStream()));
                ipAddress = in.readLine().trim();
                /* IF not connected to internet, then
                 * the above code will return one empty
                 * String, we can check it's length and
                 * if length is not greater than zero, 
                 * then we can go for LAN IP or Local IP
                 * or PRIVATE IP
                 */
                if (!(ipAddress.length() > 0)) {
                    try {
                        InetAddress ip = InetAddress.getLocalHost();
                        System.out.println((ip.getHostAddress()).trim());
                        ipAddress = (ip.getHostAddress()).trim();
                    } catch(Exception exp) {
                        ipAddress = "ERROR";
                    }
                }
            } catch (Exception ex) {
                // This try will give the Private IP of the Host.
                try {
                    InetAddress ip = InetAddress.getLocalHost();
                    System.out.println((ip.getHostAddress()).trim());
                    ipAddress = (ip.getHostAddress()).trim();
                } catch(Exception exp) {
                    ipAddress = "ERROR";
                }
                //ex.printStackTrace();
            }
            System.out.println("IP Address: " + ipAddress);
        }
    }
    

    ACTUAL VERSION: This stopped working

    Hopefully this snippet might help you to achieve this :

    // Method to get the IP Address of the Host.
    private String getIP()
    {
        // This try will give the Public IP Address of the Host.
        try
        {
            URL url = new URL("http://automation.whatismyip.com/n09230945.asp");
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String ipAddress = new String();
            ipAddress = (in.readLine()).trim();
            /* IF not connected to internet, then
             * the above code will return one empty
             * String, we can check it's length and
             * if length is not greater than zero, 
             * then we can go for LAN IP or Local IP
             * or PRIVATE IP
             */
            if (!(ipAddress.length() > 0))
            {
                try
                {
                    InetAddress ip = InetAddress.getLocalHost();
                    System.out.println((ip.getHostAddress()).trim());
                    return ((ip.getHostAddress()).trim());
                }
                catch(Exception ex)
                {
                    return "ERROR";
                }
            }
            System.out.println("IP Address is : " + ipAddress);
    
            return (ipAddress);
        }
        catch(Exception e)
        {
            // This try will give the Private IP of the Host.
            try
            {
                InetAddress ip = InetAddress.getLocalHost();
                System.out.println((ip.getHostAddress()).trim());
                return ((ip.getHostAddress()).trim());
            }
            catch(Exception ex)
            {
                return "ERROR";
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:00
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    
    try(final DatagramSocket socket = new DatagramSocket()){
      socket.connect(InetAddress.getByName("8.8.8.8"), 10002);
      ip = socket.getLocalAddress().getHostAddress();
    }
    

    This way works well when there are multiple network interfaces. It always returns the preferred outbound IP. The destination 8.8.8.8 is not needed to be reachable.

    Connect on a UDP socket has the following effect: it sets the destination for Send/Recv, discards all packets from other addresses, and - which is what we use - transfers the socket into "connected" state, settings its appropriate fields. This includes checking the existence of the route to the destination according to the system's routing table and setting the local endpoint accordingly. The last part seems to be undocumented officially but it looks like an integral trait of Berkeley sockets API (a side effect of UDP "connected" state) that works reliably in both Windows and Linux across versions and distributions.

    So, this method will give the local address that would be used to connect to the specified remote host. There is no real connection established, hence the specified remote ip can be unreachable.

    Edit:

    As @macomgil says, for MacOS you can do this:

    Socket socket = new Socket();
    socket.connect(new InetSocketAddress("google.com", 80));
    System.out.println(socket.getLocalAddress());
    
    0 讨论(0)
  • 2020-11-22 03:07

    This could be a bit tricky in the most general case.

    On the face of it, InetAddress.getLocalHost() should give you the IP address of this host. The problem is that a host could have lots of network interfaces, and an interface could be bound to more than one IP address. And to top that, not all IP addresses will be reachable outside of your machine or your LAN. For example, they could be IP addresses for virtual network devices, private network IP addresses, and so on.

    What this means is that the IP address returned by InetAddress.getLocalHost() might not be the right one to use.

    How can you deal with this?

    • One approach is to use NetworkInterface.getNetworkInterfaces() to get all of the known network interfaces on the host, and then iterate over each NI's addresses.
    • Another approach is to (somehow) get the externally advertized FQDN for the host, and use InetAddress.getByName() to look up the primary IP address. (But how do you get it, and how do you deal with a DNS-based load balancer?)
    • A variation of the previous is to get the preferred FQDN from a config file or a command line parameter.
    • Another variation is to get the preferred IP address from a config file or a command line parameter.

    In summary, InetAddress.getLocalHost() will typically work, but you may need to provide an alternative method for the cases where your code is run in an environment with "complicated" networking.


    I am able to get all the IP addresses associated all Network Interfaces, but how do i distinguish them?

    • Any address in the range 127.xxx.xxx.xxx is a "loopback" address. It is only visible to "this" host.
    • Any address in the range 192.168.xxx.xxx is a private (aka site local) IP address. These are reserved for use within an organization. The same applies to 10.xxx.xxx.xxx addresses, and 172.16.xxx.xxx through 172.31.xxx.xxx.
    • Addresses in the range 169.254.xxx.xxx are link local IP addresses. These are reserved for use on a single network segment.
    • Addresses in the range 224.xxx.xxx.xxx through 239.xxx.xxx.xxx are multicast addresses.
    • The address 255.255.255.255 is the broadcast address.
    • Anything else should be a valid public point-to-point IPv4 address.

    In fact, the InetAddress API provides methods for testing for loopback, link local, site local, multicast and broadcast addresses. You can use these to sort out which of the IP addresses you get back is most appropriate.

    0 讨论(0)
提交回复
热议问题