Solved

Getting the Machine IP through an Applet

Posted on 1998-06-09
5
203 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 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

729 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