private String client = "Unknown";
private static final String hostname = "www.google.at";
private InetAddress ia = null;
private Socket aSocket = null;
private int portnumber = 80; // whatever port # your web server uses;
public String getLocalIp() {
try {
// try getting the client address using InetAddress. From my
// experience this mostly returns the loopback ip. (127.0.0.1)
ia = InetAddress.getLocalHost();
this.client = ia.getHostAddress();
// Did we get the loopback address?
if (this.client.compareTo("127.0.0.1") == 0) {
// yes, so use a socket to compute the ip address instead
try {
// try to determine the web server to connect to for the socket
// connection below.
// Connect the socket to the webserver the applet came from.
aSocket = new Socket(hostname, portnumber);
ia =
aSocket.getLocalAddress();
this.client = ia.getHostAddress();
} catch (Exception err) {
this.client = "Client Unknown";
}
}
} catch (Exception err) {
this.client = "Client Unknown";
}
try {
aSocket.close();
} catch (Exception err) {
}
return this.client;
}