#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    How to get ip address of a website?


    Hi,
    I would like to know how to get the IP address of a url using java program.

    In short, Forward DNS look-up through java code..

    Thanks in Advance,
    Anoop
  2. #2
  3. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    Originally Posted by anoopsp
    I would like to know how to get the IP address of a url using java program.
    There are 3 methods :
    URL:
    [I dont think it would work but try it!]
    http://download.oracle.com/javase/1....html#getHost()

    InetAddress:
    http://download.oracle.com/javase/1....etAddress.html

    Socket:
    http://download.oracle.com/javase/1....et/Socket.html
    After getting host name just create a socket at defined port(Refer URL and Socket) if URL doesnot specify port then it would be 80 by default, You can get details of remote host directly using socket api and then close it..
    no one can become perfect by merely ceasing the act
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Re:How to get ip address of a website?


    I tried 3 options.. seems like nothing is working..

    Code
    ====
    InetAddress addr = InetAddress.getByName("url");
    System.out.println(addr.getHostName());

    Output = url

    Code
    ====
    URL url=new URL("url");
    System.out.println(url.getHost());

    Output = url

    Code
    ====
    Socket s=new Socket("url", 80) ;

    Output = Exception occurred..
    Exception in thread "main" java.net.ConnectException: Connection timed out: connect

    Regards,
    Anoop.
  6. #4
  7. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    try putting "url" in browser does it ???

    have a go through
    Uniform Resource Locator
    no one can become perfect by merely ceasing the act
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0
    Hi,
    Since i could not able to mention the actual url in the forum, i just typed "url" instead.

    in my code url=www_oreilly_com

    Sorry for the inconvenience..

    Regards,
    Anoop.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2009
    Location
    London, ON
    Posts
    322
    Rep Power
    219
    try [code] tags. I don't THINK it will strip/complain about addresses in them. Might, might not.
    <Tetrad> the program I just wrote 1) compiled the first time without any errors and 2) worked like it was supposed to
    <Tetrad> I don't know whether to be proud or scared to death
    Originally Posted by DaWei_M
    That covers a multitude of your sins, right there.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0
    Can u give the URL which u tried. I tried with many URL and i dint get the IP of it.. i just got the url itself
  14. #8
  15. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    are you connected to internet ???

    post the code and stacktrace so that one can know what error you get.
    no one can become perfect by merely ceasing the act
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Re:How to get ip address of a website?


    Below the code
    ============

    public static void main(String args[]) throws IOException {
    InetAddress addr = InetAddress.getByName("www_dot_oreilly_dot_com");
    System.out.println(addr.getHostName());

    URL url=new URL("http://oreilly_dot_com");
    System.out.println(url.getHost());

    Socket s=new Socket("www_dot_oreilly_dot_com", 80) ;
    }
  18. #10
  19. Santosh Vaza
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Location
    JHome
    Posts
    356
    Rep Power
    17
    i think you have completely ignored my previous post
    How to get ip address of a website?

    Comments on this post

    • anoopsp agrees
    no one can become perfect by merely ceasing the act
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Re: How to get ip address of a website


    Here is the Code.


    InetAddress addr = InetAddress.getByName("mail.yahoo.com");
    Socket socket=new Socket(addr, 80);
    System.out.println(socket.getInetAddress());
    System.out.println(socket.getRemoteSocketAddress());
    socket.close();

    Thanks for all of your help

    Regards,
    Anoop

    Comments on this post

    • codeJ agrees
  22. #12
  23. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    If you just print out an InetAddress instance its toString method is called implicitly, giving you the URL and the IP address. This is what happens on this line:
    java Code:
    System.out.println(socket.getInetAddress());
    If you replace it with this:
    java Code:
    System.out.println(addr.getHostAddress());

    You get a String representing the IP address. You do not need to create a socket for this. because the getByName() method you call in this piece of code:
    java Code:
    InetAddress.getByName("mail.yahoo.com");
    connects to a nameservice (at least it tries to) and resolves the IP address of the URL you provided.

    Comments on this post

    • anoopsp agrees
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams

IMN logo majestic logo threadwatch logo seochat tools logo