How can I get the real local host IP address in an applet?

John Zukowski

Applet security restrictions do not let you get this in an untrusted applet via InetAddress.getLocalHost().

However, you can get this address by creating a Socket connection back to the web server from which you came and asking the Socket for the local address:

    URL url = getDocumentBase();
    String host = url.getHost();
    Socket socket = new Socket(host, 80);
    InetAddress addr = socket.getLocalAddress();
    String hostAddr = addr.getHostAddress();
    System.out.println("Addr: " + hostAddr);