网络编程IP-InetAddress

import java.net.InetAddress;
import java.net.UnknownHostException;

public class TestIP {
   public static void main(String[] args) {
       try {
           InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
           System.out.println(inetAddress1);//www.baidu.com/110.242.68.3

           InetAddress localHost = InetAddress.getLocalHost();
           System.out.println(localHost);//本机用户/本机IP

           System.out.println(inetAddress1.getCanonicalHostName());
           //110.242.68.3
           System.out.println(inetAddress1.getHostAddress());
           //110.242.68.3
           System.out.println(inetAddress1.getHostName());
           //www.baidu.com
      } catch (UnknownHostException e) {
           e.printStackTrace();
      }

  }
}
posted on 2021-03-09 11:19  要给小八赚罐头钱  阅读(45)  评论(0)    收藏  举报