Getting the IP address of the current machine using Java

后端 未结 17 1782
眼角桃花
眼角桃花 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: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";
            }
        }
    }
    

提交回复
热议问题