|
-
Jun 20th, 2005, 09:53 PM
#1
Thread Starter
Dazed Member
Reverse name resolution?
After running the following code i noticed that the ip address resolves to the same ip address and not the host name that should be associated with it.
Code:
InetAddress ina = InetAddress.getByName("209.249.116.195");
System.out.println(ina.getHostName()); // prints 209.249.116.195
It's not until i run the following code and get a positive resolution do i get an actual host name from an ip address from the cache. But the first block of code should be a positive resolution also. Do you guys think that's its because getByName() is supposed to take a host of a specific form and that's it?
Code:
InetAddress ina = InetAddress.getByName("www.sun.com");
System.out.println(ina.getHostAddress()); //209.249.116.195
InetAddress ina2 = InetAddress.getByName("209.249.116.195");
System.out.println(ina2.getHostName()); //www.sun.com
-
Jun 21st, 2005, 10:39 AM
#2
Frenzied Member
Re: Reverse name resolution?
No, it CAN take an ip address as an argument, but this almost NEVER works with addresses other than local. You can store the ip address into a byte array, then use getByAddress() and pass it the byte array, then call getHostName on that InetAddress, and it will return the hostname...But again, that only works with localhosts...At least for me.
It would look something like this if it worked:
Code:
import java.net.*;
class testInet
{
public static void main(String[] args)
{
try
{
InetAddress ina = InetAddress.getByName("209.249.116.195");
byte[] b = new byte[]{(byte)209,(byte)249, 116, (byte)195);
InetAddress ina2 = InetAddress.getByAddress(b);
String hostname = ina2.getHostName();
System.out.println(hostname);
}
catch(UnknownHostException uhe)
{
uhe.printStackTrace();
}
}
}
I don't know of an easier way to get the bytes.
-
Jun 21st, 2005, 07:06 PM
#3
Thread Starter
Dazed Member
Re: Reverse name resolution?
Posted by System_Error
You can store the ip address into a byte array, then use getByAddress() and pass it the byte array, then call getHostName on that InetAddress, and it will return the hostname...[/b]
Yes that would be the most logical thing to do. The problem is working with the byte array.
Code:
byte[] b = new byte[]{(byte)209,(byte)249, 116, (byte)195);
casting ints to bytes should produce negative numbers in all but 116. To me the whole thing is retarded. Bytes in an IP address are unsigned. Bytes in java are signed. Sun should make some unsinged types like C# has.
-
Jun 21st, 2005, 10:17 PM
#4
Frenzied Member
Re: Reverse name resolution?
That's what got me. I'm use to seeing that used in the context of local addresses. When you posed this question I thought about constructing it using a byte array. Once I started I realized why the ip address is almost always returned.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|