Get the correct local IP address from java applet

Solution 1:

As you've already discovered, a computer may very well have several network interfaces with different IP addresses and it's a little bit difficult to guess which one you consider to be "correct", as they are all actually correct.

My crystal ball suggest me that you mean the IP address, which the client is using to connect to the server, from which the applet was loaded. If so, you have at least two possibilities:

  • On the server, you can embed the applet on a dynamically generated HTML page and add the client's IP address as an applet parameter. At least if you're not doing HTTP over a proxy, the web server should be able to determine the client's IP address and pass it on to the applet.

  • In the applet, you can open a TCP socket to the web server from which you loaded the applet and check which local address is being used for the connection:

.

Socket s = new Socket("www", 80);
InetAddress ip = s.getLocalAddress();
s.close();