Can anyone tell me what is difference between Internal IP Address and External IP Address? How to get both in any programming language like Java, C# or Adobe AIR?
Asked
Active
Viewed 2,766 times
0
-
1For external IP address in Java, look here: http://stackoverflow.com/questions/2939218/getting-the-external-ip-address-in-java – MByD Apr 04 '11 at 20:15
-
What about internal IP? How to get it in Java? – Mudasir Bhutto Apr 04 '11 at 20:26
-
Its C#, I'd asked you for Java :) – Mudasir Bhutto Apr 04 '11 at 20:56
2 Answers
1
Internal IP address is the address from your network:
IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName());
IPAddress curAdd = heserver.AddressList[0];
curAdd.ToString();
Your external IP address is the address from your ISP
string ip = new
System.Net.WebClient()
.DownloadString(("http://www.whatismyip.com/automation/n09230945.asp"));

SQLMason
- 3,275
- 1
- 30
- 40
1
You can use the following code (in java) to get the local IP address:
public String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface ni = en.nextElement();
for (Enumeration enumIpAddr = ni.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { //ignore 127.0.0.1
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
}
return null;
}

MByD
- 135,866
- 28
- 264
- 277
-
Thanks a lot MByd. Please give me a code for geeting machine name in java too.... – Mudasir Bhutto Apr 04 '11 at 22:06