How can I get the users IP and hostname using Java - java

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.

Related

getting only url from ip address in java

The following java code will convert an IP Address to its host.
InetAddress addr = InetAddress.getByName("31.13.78.13");
String host = addr.getHostName();
System.out.println(host);
Here host will be printed as "edge-star-shv-01-sit4.facebook.com". The first portion may be the address of a data center of facebook. I only want the "facebook.com" portion. I could not find any built in method in InetAddress library that can sort this out. I can solve the problem using substring method but this is not a good way because I want a generalized approach. Is there any way where I can get "facebook.com" instead of "edge-star-shv-01-sit4.facebook.com" by taking "31.13.78.13" as input?
You can't!
I run multiple websites on one IP adress. So if you would use a script as you discribe it here, what should it return?
The only thing is to get the hostname of the server with the selected IP, this can contain the website name , as in your case, but it doesn't have to. Imagine a server with hostname "example.com" and IP 1.1.1.1, or whatever, on this machine a website in running with url "stackoverflow.com" then you expect when tracing the IP adress to get "stackoverflow.com" but in fact you would get "example.com"

Android resolve host name to ip address translation

How can I resolve a host name to an IP address on an Android phone? I do not have access to the /system/etc/hosts file.
InetAddress can do that for you:
String ip = InetAddress.getByName("stackoverflow.com").getHostAddress();
// result e.g: "151.101.129.69"
This also needs to be done in a background-thread.

How to get URL from an IP address in Java?

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.

How to display and store Public IP address of a system in Java

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"));

why the two IP differ?

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

Categories

Resources