InetAddress类(IP地址)

package day1;

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

public class InetAddressDemo {
    //InetAddress类,对应IP地址
    //IP地址和端口号组合得到 网络套接字,即Socket
    public static void main(String[] args) {
        try {
            InetAddress inet1=InetAddress.getByName("192.168.0.0");
            System.out.println(inet1);
            InetAddress inet2=InetAddress.getByName("www.baidu.com");
            System.out.println(inet2.getHostName());
            System.out.println(inet2.getHostAddress());
            InetAddress inet3=InetAddress.getLocalHost();
            System.out.println(inet3);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
posted @ 2022-11-28 20:19  fighterk  阅读(46)  评论(0)    收藏  举报