How do i get the Wide Area Network of my computer with Java? I try with this:
ServerSocket ss = new ServerSocket(port);
System.out.println(ss.getInetAddress().getHostAddress());
//wich return 0.0.0.0
then i try with this:
System.out.println(InetAddress.getLocalHost().toString());
//which return keenan-a658368c/192.168.1.100 < yes it is connected to router
like the function said, it return my local IP address
How do i get the WAN IP Address? such as 118.137.43.219
You can get it from http://whatismyip.com/automation/n09230945.asp. You can open an HttpURLConnection to this site and parse output.
This Program should be helpful :
import java.net.HttpURLConnection;
public class GetExternalIp {
public static void main(String args[]) {
try {
java.net.URL url = new java.net.URL(
"http://whatismyip.com/automation/n09230945.asp");
java.net.HttpURLConnection con = (HttpURLConnection) url
.openConnection();
java.io.InputStream stream = con.getInputStream();
java.io.InputStreamReader reader = new java.io.InputStreamReader(
stream);
java.io.BufferedReader bReader = new java.io.BufferedReader(reader);
System.out.print("Your IP address is " + bReader.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Referenced from :
http://www.daniweb.com/forums/thread192872.html
http://www.coderanch.com/t/411356/java/java/Public-IP-Address-time-limit