Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

networking - Get the correct local IP address from java applet

I would like to determine the local IP address from my java applet. The problem is when there are several IP adresses on the same machine, which has LAN and internet connections (palm, VMWare...).

Here is my test :

    public static void main(String[] args) {
      try {
        String hostName = InetAddress.getLocalHost().getHostName();
        System.out.println("HostName = " + hostName);
        System.out.println("HostAddressLocal = " +
          InetAddress.getLocalHost().getHostAddress());
        InetAddress[] inetAddresses = InetAddress.getAllByName(hostName);
        for (InetAddress inetAddress : inetAddresses) {
          System.out.println("hostAddress = " + inetAddress.getHostAddress());
        }
      } catch (Exception e) {
          e.printStackTrace();
      }
    }

The result is :

    HostName = xxxx
    HostAddressLocal = xx.xx.xx.xx
    hostAddress = 10.10.11.51
    hostAddress = 192.168.23.1
    hostAddress = 192.168.106.1

where xx.xx.xx.xx isn't the correct address. The correct is 10.10.11.51.


EDIT in response to jarnbjo :

Your crystal ball say the truth. You've understand my problem. The client can connect through a proxy so I can not use your first point. If I execute this code below on my computer :

    Socket s = new Socket("www.w3c.org", 80); 
    InetAddress ip = s.getLocalAddress(); 
    System.out.println("Internet IP = " + ip.toString()); 
    s.close(); 

I have this result :

    Internet IP = /127.0.0.1 

And not 10.10.11.51

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...