Solved

IP number to IP name/domain name

Posted on 2004-10-22
203 Views
Last Modified: 2010-03-31
Hi,
I am using the following code to retrieve the IP name from a given IP number:

InetAddress iAddress = InetAddress.getByName("64.68.82.173");
String ipName = iAddress.getCanonicalHostName();

But it is returning only the id address which is not true, it is suppose to return "crawler14.googlebot.com" (this is what I am getting when I tired from other online IP host names finders, for ex: http://www.hcidata.co.uk/host2ip.htm

Any idea? I have also tried other related methods for example: getHostAddress,getAddress,

Thanks
0
Question by:javagirip
    12 Comments
     
    LVL 2

    Expert Comment

    by:Breadstick
    Is this what you're looking for?

    iAddress().getHostName();
    0
     
    LVL 2

    Expert Comment

    by:Breadstick
    Whoops... sorry, nevermind.
    0
     

    Author Comment

    by:javagirip

    But iAddress().getHostName() is also returning "64.68.82.173" not the host name/domain name, I need to get something like "crawler14.googlebot.com". Did you test it? what is it returning to you?

    Thanks
    0
     
    LVL 2

    Expert Comment

    by:Breadstick
    Yeah, I tried it.  The following works:
    ---
            byte[] ipAddr = new byte[]{127, 0, 0, 1};

            InetAddress addr = InetAddress.getByAddress(ipAddr);

            System.out.println(addr.getHostAddress());
            System.out.println(addr.getHostName());
    ---

    I couldn't get it working with 64.68.82.173 because I couldn't put 173 into the byte array, because the largest value a byte can hold is 127.  I'm not sure how to get around this...

    Experts?
    0
     

    Author Comment

    by:javagirip
    Thank, Good try. Any one else know how to get the hostname from an IP address? thanks
    0
     
    LVL 2

    Expert Comment

    by:ThummalaRaghuveer
    as byte in java is signed it cannot hold more than 127.

    Work around for this is just to cast it to byte say like

    byte[] ipAddr = new byte[]{(byte)128, 82, 5, (byte)244};

    This works
    0
     
    LVL 9

    Accepted Solution

    by:
    0
     
    LVL 2

    Expert Comment

    by:Breadstick
    > Here you go:
    > http://www.javaalmanac.com/egs/java.net/GetHostname.html

    I found that link too, but it doesn't work outside of localhost.  Try it.
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    Oh, I've never had that problem before. Okay -- thanks Breadstick :)
    [r.D]
    0
     

    Author Comment

    by:javagirip
    Thanks :)
    0
     
    LVL 9

    Expert Comment

    by:DrWarezz
    ta :)
    [r.D]
    0
     
    LVL 2

    Expert Comment

    by:Breadstick
    Where you able to get that to work with the googlebot?
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    857 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now