How to get the IP address from the Domain Name in Java?

后端 未结 4 998
死守一世寂寞
死守一世寂寞 2021-02-07 00:45

I am writing an application where I need the IP address. I have a domain name and I would like to know how to get the IP address from it. For example, \"www.girionjava.com\". Ho

相关标签:
4条回答
  • 2021-02-07 01:14

    This should be simple.

    InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
    for(InetAddress address : machines){
      System.out.println(address.getHostAddress());
    }
    
    0 讨论(0)
  • 2021-02-07 01:15
    InetAddress giriAddress = java.net.InetAddress.getByName("www.girionjava.com");
    

    Then, if you want the IP as a String

    String address = giriAddress.getHostAddress();
    
    0 讨论(0)
  • 2021-02-07 01:26

    (Extra mask in printing sine java considers all integers to be signed, but an IP address is unsigned)

    InetAddress[] machines = InetAddress.getAllByName("yahoo.com");
    for(InetAddress address : machines){
      byte[] ip = address.getAddress();
      for(byte b : ip){
        System.out.print(Integer.toString(((int)b)&0xFF)+".");
      }
      System.out.println();
    }
    
    0 讨论(0)
  • 2021-02-07 01:33
    InetAddress.getByName("www.girionjava.com")
    
    0 讨论(0)
提交回复
热议问题