Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Getting the Machine IP through an Applet

Posted on 1998-06-09
5
Medium Priority
?
205 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

618 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