The following snippet :
<%= InetAddress.getLocalHost() %>
gives out this : Feddy/192.168.42.194
but when I check the website ipchicken , I get this :106.193.214.75
Why the two IP differ ?
106.193.214.75 is a public IP address of your network.
192.168.42.194 is your local IP address - IP of the machine in internal network. Every machine in your network have the same public IP address.
An address 192.168.x.x is for private internal networks only. The fact you can talk to the internet means you also have a public IP address.
Its the job of your router to do network address translation so that your devices on your private network all appear with your public address.
The server is behind NAT which gives it a separate IP address locally compared to the one used on public Internet.
There are several reasons why NAT is used, including security and the limitation of available public IPv4 addresses.
192.168.xx.xx is your local ip on your network. 106.193.xxx is your external IP.
You can get both with the following code:
String hostName = InetAddress.getLocalHost().getHostName();
InetAddress[] addresses = InetAddress.getAllByName(hostName);
for (InetAddress a: addresses) {
System.out.println(a.getHostAddress());
}
The ip 192.168.42.194 is your local ip, it is given to your pc by your router.
The other ip is your WAN ip, it is given by your isp and is the ip address your router gets for connections from the outside world
Because 192.168.42.194 is your private ip, on your private network, and 106.193.214.75 your public one, assigned to your gateway by your ISP.
In JDK 1.6
List<InetAddress> addrs = new ArrayList<InetAddress>();
for(NetworkInterface ni : NetworkInterface.getNetworkInterfaces()) {
if(ni.isUp()) {
for(InetAddress addr : ni.getInetAddresses()) {
addrs.add(addr);
}
}
}
Regards,
One is your local IP address (from your router) and the other one is your IP address over Internet.
192.168 is always from a router
Related
I found code to fetch host name from IP address. The code is some what like shown below:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class GetHostName{
public static void main(String a[]){
try{
InetAddress host = InetAddress.getByName("74.125.68.94");
System.out.println(host.getHostName());
}
catch(UnknownHostException ex){
ex.printStackTrace();
}
}
}
It shows output like:
sc-in-f94.1e100.net
But when i fired this IP address (74.125.68.94) through the browser it opens up Google website.
So my question is how can i fecth URL like http://www.google.com from an IP address rather than displaying sc-in-f94.1e100.net using java?
You can't do that.
Usually, several domains can be reach on same IP. You can just identify the hostname like you already do. This is the name of the machine, equivalent to the IP, which serves the domain.
Moreover, an IP is just an address of a machine which also can serve any domain because it can be a web server or not.
You are actually doing everything right. The thing is that today many host names can live on the same server. This way you can easily convert the host name to the IP address of the server but when you attempt to do the reverse, you only get the name of the server which is in this case sc-in-f94.1e100.net.
Here is an excerpt from hcidata:
In the early years of the Internet, each sub-domain would have a unique IP address so it was common for a host machine to have only one sub domain name. Nowadays, the common practice is to have many sub-domains with the same IP address. It is also common for the domain name to be converted to the IP address of the host machine that runs the www sub domain.
I hope this will clear things up for you.
I am trying to display and store the Public IP address of a system. I used the following code for the same. i.e.,
I have imported following two statements :
import java.net.InetAddress;
import java.net.UnknownHostException;
try {
InetAddress iAddress = InetAddress.getLocalHost();
String currentIp = iAddress.getHostAddress();
System.out.println("Current System's IP address is : " +currentIp);
} catch (UnknownHostException e) {
System.out.println("Catch block executed. So IP address is not displayed");
}
It is displaying the output as :
"Current System's IP address is : 192.168.1.5"
But my system's public IP address (as checked in https://www.whatismyip.com/) is :
115.107.244.81
So how should I fetch and display the public IP address from .JAVA file?
https://www.whatismyip.com -> which will display the public ip address which will be known to the outside world. If you restart your system then you can see different ip address. Eventhough if you are having a static ip address the website won't show your machine ip address.
Java code output shows your system ip address which will be used for inter communication and that is your actual system ip address. you can verify that in shell/command prompt.
if your are using net from local area network then it always start with 192. something ..
Try to connect with other mode of net connection they it will show the public ip address.
Command to get ip address is ipconfig in command prompt in Windows.
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest servletRequest = attributes.getRequest();
ipAddress = String.valueOf(servletRequest.getSession().getAttribute("clientAddress"));
Once again i require some small help on how i can make use of Inet6Address on java.
As i develop a application on getting IPv4 address but now i wanna extend it to IPv6. And it seem that i cant get a IPv6 address and it keep get IPv4 address.
ANd i try to import java.net.Inet6Address;
With the existing code that i have which is show below.
public SocketAddress getInetAddress(){
return channel.getRemote();
}
The channel is using the netty project.
How can i do with Inet6Address?
As i found on this web
http://docs.oracle.com/javase/1.4.2/docs/api/java/net/Inet6Address.html
public InetSocketAddress getInetAddress(){
Inet6Address ipv6 = (Inet6Address)channel.getRemoteAddress.**getAddress();**
return channel.getRemote();
}
Can i also ask if the .getAddress() cant be use, Can anyone help me on this issues?
Like do i need to download a jar file to work on this?
The version of java i am using is java 6.
From Swift
if you are ok - try to use
Java-ipv6
what is the type of your channel? never used 'netty', but in sun's nio world, assuming sa is an instance of SocketAddress, just use sa.getAddress(). if sa is representing a socket on IPv4 address, you will get an instance of Inet4Address, or a Inet6Address when it is a socket on IPv6 address.
add some IPv6 bind code:
InetAddress[] addresses = InetAddress.getAllByName("localhost");
Inet6Address add6 = null;
for(InetAddress add : addresses) {
if (add instanceof Inet6Address) {
add6 = add;
break;
}
}
if (add6==null)
throw new RuntimeException("no IPv6 local address found!");
InetSocketAddress sa=new InetSocketAddress(add6, port);
...
InetAddress addr = java.net.InetAddress.getRemoteHost();
MyHost = addr.getHostName();
IPaddressString = addr.getHostAddress();
The above code returns the users local data being 127.0.0.1 and Localhost
1. If you want the IP of the remote user then you must have his Domain Name , if you want
his Domain Name then you must have his IP.
2. Now i hope you are in LAN Environment, and have configured the IPs correctly.
3. Try assigning your PC with an ip suppose 192.168.20.1 and the other pc as 192.168.20.2
4. If you have Domain Name Associated with the remote pc, then you will get his Domain Name in return by using your code.
I've been asked to activate a certain piece of code if i was in my college. So I need to find the iP of where i am to match to my colleges iP. Was wonderng how to do this in java? I have already tried a loop back interface.
By using NetworkInterface.getNetworkInterfaces() and calling getInetAddresses() on each interface, you can see all IP addresses assigned to your computer. To check if you have an IP in your university's range, you could do something like this:
boolean onCampusNetwork() {
for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = ifaces.nextElement();
for(Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements;) {
InetAddress address = addresses.nextElement();
// return true if address is in the university's range; something like:
if(address.toString().startsWith("10.0")) {
return true;
}
}
}
// None of the IP addresses were in the university's range.
return false;
}
I haven't run this code, but it should do what you need.
Isn't there supposed to be some kind of protocol for automatic proxy discovery and configuration? Does your college have this already setup? Then it would be better if your code discovered the correct settings and had an option to override the settings.
There are all kinds of websites out there that will give you your public ip (or the public IP of your gateway, which I assume is what you want). You could tell the program to make an HTTP connection to one of those sites and get the page with the info on it. Since these sites have a very predictable format, the result would be very easy to parse with a regex or two. This only works if you have an internet connection though.
Alternatively, you could have the program try to connect to one of your college's intranet servers. If it can make the connection to a site that is not accessible to the outside world, it's on the LAN.