?
Solved

Getting the Machine IP through an Applet

Posted on 1998-06-09
5
Medium Priority
?
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
fontaine earned 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 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