Different IP addresses by getLocalHost and getByName("localhost")..why so? - java

Under what circumstances does InetAddress.getLocalHost().getHostAddress() return a different IP address than InetAddress.getByName("localhost")?
On my system, one returns 192.168.0.2 while the other returns 127.0.0.1

seems, InetAddress.getLocalHost().getHostAddress() is returning your system ip and InetAddress.getByName("localhost") the loopback address.
I doubt the security manager case described by Parthian for getByName, As per InetAddress API specification for getByName():
http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getByName%28java.lang.String%29
The method throws: SecurityException - if a security manager exists and its checkConnect method doesn't allow the operation.
whereas getLocalHost() doesn't throw any such exception but returns loopback address as failsafe.
http://docs.oracle.com/javase/7/docs/api/java/net/InetAddress.html#getLocalHost%28%29
getByName() needs to connect to DNS to resolve hostname.
getByName() in this case is resolving 'localhost' from /etc/hosts(linux) or C:\Windows\System32\drivers\etc (windows). The hostname ip pair is user configurable in these files.
To check, you can provide any value to localhost, e.g: localhost 127.0.0.2 in hosts file, and getByName will return it.

According to this:
"[InetAddress.getLocalHost()] Returns the address of the local host. This is achieved by retrieving the name of the host from the system, then resolving that name into an InetAddress.
Note: The resolved address may be cached for a short period of time.
If there is a security manager, its checkConnect method is called with the local host name and -1 as its arguments to see if the operation is allowed. If the operation is not allowed, an InetAddress representing the loopback address is returned."
Probably what's happened is you're getting the loopback address because your security manager doesn't allow you to connect using the local subnet's 192 address.
InetAddress.getByName("localhost") justs asks the operating system to perform a name resolution, from what I can tell, anyway.

Related

Corba IOR decode

I know this IOR may contain multiple components.The typical IOR normally contains the IP address of the remote host, the number of the remote port on that the CORBA server is listening, a string defining the class of the remote object on which the methods will be invoked, and the object key that is used by the server ORB to identify the object.
Is it possible that when I decode an IOR it gives me HOST NAME instead of IP Address.?
that is, it resolves like Machine-abc.co.in instead of some IP Adress like 192.168.64.56
Set this to use DNS names in IORs
jacorb.dns.enable=on
Also , refer http://jacorb.org/bugzilla/show_bug.cgi?id=872

"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.

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

Java Socket on Different Machine Does Not Work

I've tried many examples on web and one of them is this:
http://zerioh.tripod.com/ressources/sockets.html
All of the server-client socket examples work fine when they are tested with 127.0.0.1
BUT it never ever EVAR works on two different computers with actual raw real IP address ("could not connect to host" on telnet and "connection timed out" when tested on java client - the server program just waits for connection)
Note:
Firewall is turned off for sure
IP address from ipconfig didn't work
IP address from myipaddress.com (which is totally different for no reason than that from ipconfig) didn't work
What is it that I'm missing?
If I can only figure this out...
Try binding on 0.0.0.0. This tells your socket to accept connections on every IP your local can accept upon.
Based on the comment where the the following snippet of code is mentioned:
requestSocket = new Socket("10.0.0.5", 2004); // ip from ipconfig
it would be better to use the hostname instead of the IP address in the constructor, as the two-parameter Socket constructor with a String argument expects the hostname as the String, and not an IP address. A lookup of the IP address is then performed on the provided hostname.
If you need to pass in an IP address, use the two-parameter constructor that accepts the InetAddress as an argument. You can then provide a raw IP address to the InetAddress.getByAddress method, as shown in the following snippet:
InetAddress addr = InetAddress.getByAddress(new byte[]{10,0,0,5});
You'll need to be careful when specifying arguments via the byte array, as bytes are signed in Java (-127 through +128), and numbers beyond this range (but valid octets of IP addresses) may have to be specified using Integer.byteValue.
Finally, it should be noted that it is important to specify the IP address of the remote machine, as visible to the client. The IP address listed at myipaddress.com may be the address of a proxy, as that is the public IP of your entire network as visible to the host server at myipaddress.com. Therefore, you ought to be specify the IP address of the remote machine that is visible to your machine and not myipaddress.com.

How to fetch all DNS entries from JAVA application?

As of now, I'm using the below code to get DNS name of the given IPAddress. Instead of fetching it for each IPAddress in the network, I want to fetch all the DNS entries (IPAddress - HostName mapping) from the DNS Server in one go. Is it possible? If so, how to do it?
InetAddress addr = InetAddress.getByName(address);
dnsname = addr.getCanonicalHostName().trim();
From a public DNS server, there is no way to pull out all the data it holds. Enumerating all the IP addresses one by one is the only solution.
If you have a special relationship with the DNS server (for instance, it is managed by your employer), you may request from the DNS administrator a right to transfer the whole zone (the DNS request known as AXFR). They may authorize your IP address or gives you a TSIG key to authentify yourself.
Then, you will have to find a way to do a zone transfer (possibly with TSIG authentication) in Java. Using these keywords, I find some code and documentation. Use a code search engine like Google Code Search or Krugle to find examples of use.
[DNS experts will probably scream "Use zone walking on NSEC" but most DNS zones are not signed with NSEC.]

Categories

Resources