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.
Related
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"
I'm trying to get the device's name using its local IP address on the network. Is this how I'm supposed to do it? ex) Arnold-PC, andoid-nnnnnnnnnn
String name = InetAddress.getByName(ip).getHostName();
System.out.println(name);
The above should give me the host's name... but instead gives me the local IP address. - 192.168.2.101
as per the documentation...
public String getHostName ()
Returns the host name corresponding to this IP address. This may or
may not be a fully-qualified name. If the IP address could not be
resolved, the numeric representation is returned instead
Why is it not able to find the host's name?
I don't know much about computer networking... so please excuse my ignorance. :P
check in the command prompt whether you can able to resolve the ip address using nsloookup
if you can't, then your DNS broken
I would like to quote the few lines from java documentation here
getCanonicalHostName() Gets the fully qualified domain name for this
IP address. Best effort method, meaning we may not be able to return
the FQDN depending on the underlying system configuration.
one more trick is to edit host file to get output (not recommended)
Have a look in this answer too
link
From the docs
If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName.
It seems like plain getHostName() won't look up the name if it can't reach out to a DNS server. The DNS server is what will give a name to the host IP address, just like a phone book. Give getCanonicalHostName() a try.
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"));
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
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.