Resolving ip-address of a hostname - java

I have the DNS server IP address and a hostname.
Using Java, how can I find the IP address of the hostname as returned by that DNS server using the IP address and the hostname?

Take a look at InetAddress and the getHostAddress() method.
InetAddress address = InetAddress.getByName("www.example.com");
System.out.println(address.getHostAddress());

You can do it like this:
for(InetAddress addr : InetAddress.getAllByName("stackoverflow.com"))
System.out.println(addr.getHostAddress());

You can use InetAddress for this. Try the below code,
InetAddress address = InetAddress.getByName("www.yahoo.com");
System.out.println(address.getHostAddress());
System.out.println(address.getHostName());

As suggested by all above, you can use
InetAddress.getByName("hostName") but this can give you a cached IP, Read the java documentation for the same.
If you want to get a IP from DNS you can use:
InetAddress[] ipAddress = DNSNameService.lookupAllHostAddr("hostName");

Related

Using InetSocketAddress correctly

I am attempting to use the InetSocketAddress that is part of Java.net. Although when I try to actually give it data for its perimeters it does not work. The documentation does not actually specify how to define the IP address and port. It states it goes as follows: InetSocketAddress(InetAddress addr, int port) , addr being the IP address. I have tried the following ways:
InetSocketAddress("0.0.0.0", 0000)
InetSocketAddress(0.0.0.0, 0000)
InetSocketAddress(0.0.0.0:0000)
Obviously none of these work (using arbitrary values), all but the last, the port works cause it's just an int but I can't figure out how to format the IP address correctly. The docs I have read over in search of a solution are as follows:
InetAddress
Inet4Address
InetSocketAddress
None of these actaully have an example of how to format the IP address (unless I'm blind of course).
InetSocketAddress wants an InetAddress object for the first parameter. So you need to send it one with something like:
InetSocketAddress(InetAddress.getByName("0.0.0.0", 0))
I know, getByName() doesn't sound like it should be used with an IP address, but it works with both an address or host name.
You can use this.
InetSocketAddress address=new InetSocketAddress(InetAddress.getByName("0.0.0.0"),0000);
InetSocketAddress has no parameter is String, int constructor

How to connect with difference Ip address

My server have many ipaddress (a full /30 block)
how to make socket with other ipaddress than default ip address
Socket socket = new Socket()
Use one the alternative constructors
e.g. Socket(java.net.InetAddress, int port)
For a server wishing to use a specific ip address and port, you should be using ServerSocket
Use bind(java.net.SocketAddress) to specify the local ip address and port.

"localhost" vs 127.0.0.1 java

Java is giving 127.0.0.1 as IP for InetAddress.getByName("localhost").getHostAddress()
But why java not gives "localhost" for InetAddress.getByName("127.0.0.1").getHostName. For later one I get "127.0.0.1" as host name. Please clarify this.
The javadoc of InetAddress.getByName(String) states
The host name can either be a machine name, such as "java.sun.com", or
a textual representation of its IP address. If a literal IP address is
supplied, only the validity of the address format is checked.
So it doesn't actually go to your hosts file (or DNS) for an IP address. It just creates a InetAddress object with both hostname and address created from the String you provided.
For your first example
InetAddress.getByName("localhost").getHostAddress()
Assuming you have a hosts file entry like
127.0.0.1 localhost
then the InetAddress object returned will have that information, ie. a hostname of localhost and an address of 127.0.0.1.
Similarly, if you had
1.2.3.4 this.is.a.name
and
InetAddress localhost = InetAddress.getByName("this.is.a.name");
The returned InetAddress would be constructed with a hostname of this.is.a.name and an address of 1.2.3.4, because it actually went and checked.

Get global IP address from java http server

I m starting a local http server using this code:
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/intro", new MyHandler());
server.setExecutor(null);
server.start();
now I want to hit the url /intro from some other server.The problem is I don't know the ip adress to hit.Doing server.getAddress() gives 0.0.0.0:8000.I want to find the global ip address.
You have to know what is your ip (cmd and ipconfig) and get IPv4 address. But remember that is only your local IP visible in your local network, so only users in the same network can see it. In global network you are identified by your internet provider IP.
InetSocketAddress(int port)
Creates a socket address where the IP address is the wildcard address and the port number a specified value.
InetSocketAddress(InetAddress addr, int port)
Creates a socket address from an IP address and a port number.
The wildcard is a special local IP address. It usually means "any" and can only be used for bind operations.The value of this IP address is 0.0.0.0.
so use another constructor of InetSocketAddress when you can put hostname
new InetSocketAddress(String hostname, int port) calls InetAddress.getByName(hostname).
Now you shall get your local ip address when you query server.getAddress()

Java. InetAddress.getLocalHost returns strange IP

I'm don't understand, why code below prints 0.0.9.229 instead 127.0.0.1. Can anybody tell me, hot to fix that?
String ha = InetAddress.getLocalHost().getHostAddress();
System.out.println(ha);
UPD:
Code running on Ubuntu
/etc/hosts
127.0.0.1 localhost
127.0.1.1 2533
InetAddress.getLocalHost() doesn't do what most people think that it does. It actually returns the hostname of the machine, and the IP address associated with that hostname. This may be the address used to connect to the outside world. It may not. It just depends on how you have your system configured.
On my windowsbox it gets the machine name and the external ip address. On my linux box it returns hostname and 127.0.0.1 because I have it set so in /etc/hosts
The problem is that my hostname will consists only of numbers and could not be resolved.
I change my /etc/hostname with characters at first position and problem has solved.
Use NetworkInterface to enumerate network interfaces; InetAddress.getLocalHost() always returns loopback.If you want to get all IP's associated with your machine use NetworkInterface then you will get 127.0.0.1 also.
Enumeration<NetworkInterface> nInterfaces = NetworkInterface.getNetworkInterfaces();
while (nInterfaces.hasMoreElements()) {
Enumeration<InetAddress> inetAddresses = nInterfaces.nextElement().getInetAddresses();
while (inetAddresses.hasMoreElements()) {
String address = inetAddresses.nextElement().getHostAddress();
System.out.println(address);
}
}

Categories

Resources