-1

I need to find User's local IP. The server is behind a router, so I cannot use any server-side method. I also don't want to use any 3rd party service like http://jsonip.appspot.com/ because I tested it and its server goes down from time to time.

Using Java, I I want to get the IP through something like

<script type="text/javascript">
        function myIP() {
            var yip2 = java.net.InetAddress.getLocalHost();
            var yip = yip2.getHostAddress();
            return yip;
        } //end myIP

        alert("your machine's local network IP is " + myIP())
    </script>

but It's not working. How can I get it work?

  • 4
    Please do not confuse JavaScript as having anything to do with Java ;) – akaIDIOT Feb 06 '13 at 11:53
  • 1
    I want to use them both :) Please read here: http://www.webdeveloper.com/forum/showthread.php?242538-Getting-local-IP-address-in-javascript –  Feb 06 '13 at 11:54
  • You can 'want' all you want, Java and JavaScript are really very different things. You can't make calls to a Java library from a JavaScript script on a web page. – akaIDIOT Feb 06 '13 at 11:56
  • Who said I am saying that Java and Javascript are the same thing??? I am using Java for what Javascript cannot do: get user's IP! –  Feb 06 '13 at 11:57
  • The issue isn't with the language but with the platform. Browser Javascript runs on people's computers without them even knowing about it, let alone giving it any permissions or rights explicitly. Therefore it has a lot of limitations when it comes to retrieving system information. Running javascript on a more lenient platform (such as server-side) would have no problems in getting the IP. – Esailija Feb 06 '13 at 11:59
  • 2
    The code snippet you posted indicates that you're confusing the two. I'm not aware of a JavaScript call that gets you the public IP of a user. This will likely involve making a request to an outside source that echoes the requester's IP back to you. That server side could well be written in Java... – akaIDIOT Feb 06 '13 at 11:59
  • @Esailija Can I use Java to help me with user's **client** IP in an asp.net application? –  Feb 06 '13 at 12:05
  • possible duplicate of [Get Client IP using just Javascript?](http://stackoverflow.com/questions/391979/get-client-ip-using-just-javascript) – Anders R. Bystrup Feb 06 '13 at 12:07
  • 1
    @AndersR.Bystrup how can it be a duplicate of that? i explicitly stated that i cannot use that solution –  Feb 06 '13 at 12:09
  • 1
    @user1761123 technically yes. If you use a certified java applet, the client has java plugin installed, and accepts it, you can get the IP. The certification costs like $1000 a year IIRC.. can't find it right now. – Esailija Feb 06 '13 at 12:10
  • @user1761123 — Duplicates mark duplicate questions not duplicate answers. It is expected that if there are multiple ways to solve a problem, then a single question will have multiple answers covering them. – Quentin Feb 06 '13 at 12:10
  • @Esailija — That would be the plugin which keeps being put on Apple and Mozilla blacklists and blocked because of the horrible security flaws it keeps having, right? :) – Quentin Feb 06 '13 at 12:11
  • 1
    @Quentin the question is not the same! I said 'not using 3rd party' and the question you sent me says 'I'm not against using a free 3rd party script' –  Feb 06 '13 at 12:12
  • @user1761123 — That's a reason to justify it not being a duplicate then. "The solution doesn't work for me" isn't. – Quentin Feb 06 '13 at 12:13
  • @Quentin sounds plausible :D – Esailija Feb 06 '13 at 12:17

3 Answers3

1

Ive used this for what you asked :

http://jsonip.appspot.com/?callback=getip

try this code :

<script type="application/javascript">
    function getip(json){
      //json.ip will have the client IP and this will alert it
      alert(json.ip);
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

and this had nothing to do with java (java-script is a different thing)

Nimrod007
  • 9,825
  • 8
  • 48
  • 71
  • I said I didn't want to use any 3rd party. I used this and appx. 3 hours a day i'm not getting a response, saying that 'daily quota reached!' –  Feb 06 '13 at 12:03
1
  1. no server-side
  2. no third-party

Sounds like you are "stuck" with what you have already provided. Your script actually got me curious because on first glance it did seem you were mixing server and client (java and javascript) + all the comments pointed to it. But curiosity is good thing :)

Apparently, though not really reliable you can call Java methods in supporting browsers. Java is client side too (the concept that was lost).

I will copy and paste this, I've never tried (because I've never been limited the same way as you are):

if (java && java.net)
ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
else ip = 'unknown';

Taken from About.com - which also describes what you need to do to at least give it a chance to work (upload, can't do it from local).


UPDATE:

Forget it. I'm not a java guy (in the first place), as @Esailija has noted, the script above will not (no longer) work:

2.1.5 Deprecated Functionality: the Global Packages, java and netscape Keywords

The Mozilla family of browsers has historically provided support for access to the Java language from JavaScript even on web pages that don't contain Java applets. In this browser family, there are global java, netscape and Packages keywords available to JavaScript code which allow calling static methods, accessing static fields, and creating new instances of Java classes in similar fashion to the per-applet Packages keyword above.

The semantics of these keywords becomes problematic when more than one applet is available on the web page. If you want to access one particular applet's user-defined classes (for example, in a com.mycompany package), how would the global Packages keyword know which applet to refer to? The new Java Plug-In also supports attaching more than one Java virtual machine instance to the web browser for executing applets. The semantics of these global keywords becomes even more complicated in this situation.

For this reason, the global java, netscape and Packages JavaScript keywords are deprecated. They continue to function in the Firefox browser, but it is strongly recommended to transition existing code using them to use the new per-applet Packages keyword. It is not possible to access user-defined classes using these global keywords; attempts to do so will yield undefined results.

Reference here

Community
  • 1
  • 1
EdSF
  • 11,753
  • 6
  • 42
  • 83
  • You don't need to upload anything, any browser you might use has a javascript console to run javascript in the context of the current page (in IE since IE8) - and `java` is `undefined` in all browsers I tested (Chrome, Firefox, IE, Safari and Opera). – Esailija Feb 06 '13 at 12:50
  • @EdSF Thanks, at least you understood the question. The code I have provided is pretty much the same as the one you pasted, right? I just cannot seem to adapt it to my page –  Feb 06 '13 at 12:53
  • 1
    @user1761123 pretty much the same, it just adds a check to see if java && java.net are enabled (I'm assuming this is the java plugin) on the browser + shorter code to get to the raw IP. – EdSF Feb 06 '13 at 12:59
  • @Esailija agreed just pointing out what that article mentioned to give it some shot (given all its limitations). – EdSF Feb 06 '13 at 13:06
  • if this is a java plugin rather than some magical javascript, then this will not work for normal applets but signed ones only. – Esailija Feb 06 '13 at 13:09
  • @Esailija my curious nature...I think [this is why](http://jdk6.java.net/plugin2/liveconnect/#DEPRECATED_FUNCTIONALITY) – EdSF Feb 06 '13 at 13:59
  • @EdSF Thanks, so I guess there's no way I can use Java to achieve this. –  Feb 06 '13 at 14:22
  • @user1761123 That would be more question for a Java dev (am .Net/ASP.Net). Judging by what the above says, you still "can" on a per applet basis. "How" isn't something I can help you with unfortunately...and it doesn't sound practical (Mozilla family only). – EdSF Feb 06 '13 at 14:36
0

The server is behind a router

Configure the router to add the X-Forwarded-For header. That contains the IP (or a chain of IPs in a "proper" implementation) the router forwarded the request for, I.E. the client IP. Now, the client IP could of course be another proxy as well but yeah.

Esailija
  • 138,174
  • 23
  • 272
  • 326
  • I'm not the owner of the server, i'm just developing an application for them. –  Feb 06 '13 at 12:21
  • @user1761123 ok.. have the owner of the server configure it – Esailija Feb 06 '13 at 12:22
  • thank you for your friendly advice, but i didn't say there's isn't any X-Forwarded-For header. The thing is that i always get **192.168.2.1** that is the router's gateaway –  Feb 06 '13 at 12:33
  • @user1761123 you get `192.168.2.1` for IP or in the `X-Forwarder-For` header? – Esailija Feb 06 '13 at 12:38
  • Yes. Please read my previous question regarding this: http://stackoverflow.com/questions/13177134/how-to-get-users-public-ip-if-the-server-is-behind-a-router –  Feb 06 '13 at 12:40
  • @user1761123 in that case my first comment applies. If you don't want to use 3rd party server, you must have the router configured properly. – Esailija Feb 06 '13 at 12:42