0

My code always return the loopback ip instead of my IP whick is 192.168..

try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());
          } catch (UnknownHostException e) {

            e.printStackTrace();

          }
Eleven
  • 339
  • 2
  • 6
  • 20

1 Answers1

0

There are some websites, that their sole piece of data is one line, which includes your public IP address.

Try something like:

URLConnection con = new URL("http://www.myipaddress.com").openConnection();
String ip = new BufferedReader(new 
                    InputStreamReader(con.getInputStream())).readLine();
Mordechai
  • 15,437
  • 2
  • 41
  • 82