Results 1 to 4 of 4

Thread: Reverse name resolution?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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.

  4. #4
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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
  •  



Click Here to Expand Forum to Full Width