0

I am trying to display and store the Public IP address of a system. I used the following code for the same. i.e., I have imported following two statements :

 import java.net.InetAddress;
 import java.net.UnknownHostException;

  try {
        InetAddress iAddress = InetAddress.getLocalHost();            
        String currentIp = iAddress.getHostAddress();
        System.out.println("Current System's IP address is : " +currentIp); 
        } catch (UnknownHostException e) {
              System.out.println("Catch block executed. So IP address is not displayed");
            }

It is displaying the output as :

 "Current System's IP address is : 192.168.1.5"

But my system's public IP address (as checked in https://www.whatismyip.com/) is :

 115.107.244.81

So how should I fetch and display the public IP address from .JAVA file?

user207421
  • 305,947
  • 44
  • 307
  • 483
Mahan
  • 371
  • 1
  • 4
  • 11
  • 1
    See this : http://stackoverflow.com/questions/2381316/java-inetaddress-getlocalhost-returns-127-0-0-1-how-to-get-real-ip – PaulF Aug 03 '15 at 12:07
  • 1
    http://stackoverflow.com/a/12107979/1225526 – Karthigeyan Vellasamy Aug 03 '15 at 12:22
  • I dont think you need, the first link of Paul, that though correct, is for a stand alone app. Looking at comments seems you want the IP of clients, on a web app, follow Karthigeyan's http://stackoverflow.com/questions/12107739/how-to-determine-by-what-ip-address-my-website-has-been-accessed/12107979#12107979 and dont worry if it shows a 192.xx address during testing. On the real internet the client IP will be recorded be correct, to test that put your code on a server and access with the public IP/ url of that server. – tgkprog Aug 03 '15 at 13:59
  • What does MySQL have to do with it? – user207421 Aug 03 '15 at 23:47
  • @tkgprog What comments? There's nothing about servlets or webapps here. What part of 'current system's IP address' don't you understand? – user207421 Aug 03 '15 at 23:50

3 Answers3

1

https://www.whatismyip.com -> which will display the public ip address which will be known to the outside world. If you restart your system then you can see different ip address. Eventhough if you are having a static ip address the website won't show your machine ip address.

Java code output shows your system ip address which will be used for inter communication and that is your actual system ip address. you can verify that in shell/command prompt.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • Hi Shriram, actually I am using Play Framework. For that I am using "activator-1.3.2" which uses JBoss-netty server (default port number:9000). In this project when end-user sign up to my web site, I want to store the user's IP address in my table. So how should I go for that...? – Mahan Aug 03 '15 at 12:04
0

if your are using net from local area network then it always start with 192. something ..

Try to connect with other mode of net connection they it will show the public ip address. Command to get ip address is ipconfig in command prompt in Windows.

  • Wrong. Not always: only if the network follows IANA's directives and if the network uses the 16-bit block name, and then in that case your answer is incomplete as the name is `192.168.xxx.yyy` and not `192.xxx.yyy.zzz`. See [Private network on Wikipedia](https://en.wikipedia.org/wiki/Private_network). – Olivier Grégoire Aug 03 '15 at 23:52
-1
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
            HttpServletRequest servletRequest = attributes.getRequest();
            ipAddress = String.valueOf(servletRequest.getSession().getAttribute("clientAddress"));
user207421
  • 305,947
  • 44
  • 307
  • 483
Rohith K
  • 1,433
  • 10
  • 15
  • Completely wrong from start to finish. There's nothing in the question about servlets; this code doesn't give the host's own public IP address; and there is nothing in the Servlet Specification that says that the servlet session will contain an entry called `"clientAddress"`. – user207421 Aug 03 '15 at 23:46