Solved

Getting the Machine IP through an Applet

Posted on 1998-06-09
5
200 Views
Last Modified: 2013-11-23
I need to obtain the IP address of the machine where the Applet is running and send it to the server, to keep track of the user? I could not find any method relating to my problem in URL class. Can anyone help me with some example, just how to get the IP from an applet?
Thanks.
0
Comment
Question by:arooj
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 100 total points
ID: 1222823
It isn't possible to obtain the IP address of the machine where the Applet is running.
Indeed, it you try the following code:

addr = InetAddress.getLocalHost();
ip = addr.getAddress();
ipStr = addr.getHostAddress();

you will see that you will always obtain 127.0.0.1. This is normal. This is because an applet
can only connect to the machine it is coming from. If the connection to the host is not allowed,
127.0.0.1 is returned (this is explicitely written in the source of InetAddress.java). Here,
because of the security restriction, the loopback address is returned. Although you sign the applet, you can't have it. You should note that Javascript, which is a language dedicated to the "programming" of a Web browser is also not allowed to give you the IP address.

Here is a link at Netscape about how to sign:

http://developer.netscape.com/docs/manuals/signedobj/signtool/

A workaround is to send an HTTP request back to the server to a CGI script or servlet.
The IP comes indeed in the header of the HTTP request, and a CGI or servlet can get
it. Please refer to the explanation of "Tracking User Time and Date" about this subject.
In particular, the getRemoteAddr() method.

You should note that I already answered to exactly the same question in the past. If you
want to read the discussion I had at this time with whitmoret, please go here:

http://www.experts-exchange.com/topics/comp/lang/java/Q.10050502

0
 

Expert Comment

by:Jose061198
ID: 1222824
You can use Java native interface.
0
 

Expert Comment

by:Jose061198
ID: 1222825
Sorry: You can't use Java native interface in applets.
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1222826
You can, but this also requires applet signing. But if you sign, you can directly use
getHostAddress() for example, so that you don't need to write the machine-specific code
needed by JNI.
0
 
LVL 5

Expert Comment

by:fontaine
ID: 1222827
The following code works, at my big surprise:

import java.awt.*;
import java.net.*;
import java.applet.*;

public class HostName extends Applet {
  public void paint(Graphics g) {
    try {
      InetAddress localhost = InetAddress.getLocalHost();
      g.drawString("IP: " + localhost.getHostAddress(), 20, 20);
      g.drawString("Hostname: " + localhost.getHostName(), 20, 40);
    }
    catch(UnknownHostException e) {
      g.drawString("Error: Unknown host", 20, 20);
    }
  }
}

This is a surprise because the implementation of getLocalHost() in SUN JDK clearly
states (InetAddress.java line 519):

"/* make sure the connection to the host is allowed: if yes,
 * return the "real" localHost; if not, return loopback "127.0.0.1"  
 */

 SecurityManager security = System.getSecurityManager();
 ..."

As an applet can only connect to the host it originates from, the loopback address *should*
be returned!

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 56
rhino JavaScript import, load 25 82
servlet doXXX methods 3 49
eclipse buid path vs tomcat lib path 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question