Hal said:
I know there are different ways to get a computers IP address. I'd like to
have a Java program find out a computer's IP address for the Internet, not
for any LAN it's on. Is there a way I can do this easily or by finding the
gateway's or proxy's eternal IP address?
There are not even a 100% safe way.
You will need to ask an external server what IP it see.
Below are a small example using a danish service.
Arne
============================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ExternIP {
public static String getIP() throws Exception {
URLConnection uc = (new
URL("
http://www.myip.dk/")).openConnection();
BufferedReader br = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
StringBuilder sb = new StringBuilder("");
String line;
while((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
Pattern p = Pattern.compile("(?:<title>Your IP:
)(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:</title>)");
Matcher m = p.matcher(sb.toString());
m.find();
return m.group(1);
}
public static void main(String[] args) throws Exception {
System.out.println(ExternIP.getIP());
}
}