Solved

Getting the Machine IP through an Applet

Posted on 1998-06-09
5
197 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
Comment Utility
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
Comment Utility
You can use Java native interface.
0
 

Expert Comment

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

Expert Comment

by:fontaine
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now