黑马程序员:IP地址:InetAddress类的介绍(无构造函数)

时间:2021-02-10 18:11:34

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

InetAddress对象,无构造函数,有非静态方法,

通过其静态方法返回实例对象

InetAddress i = InetAddress.getLocalHost();//获取本地主机信息,包含主机名和主机地址
InetAddress i =  InetAddress.getByName("www.baidu.com");//也可以写IP地址
InetAddress[] i =  InetAddress.getAllByName("www.baidu.com");//主机若存在多个IP的之后使用这个
i.getHostAddress(); //获取主机地址
i.getHostName(); //获取主机名,当没解析出主机名时,和主机地址一样

示例:
import java.net.InetAddress;
public class InetAddressDemo {


	public static void main(String[] args) throws Exception
	{
		InetAddress[] i = InetAddress.getAllByName("www.baidu.com");
		for(InetAddress ip : i)
		{
			System.out.println(ip.getHostAddress());
			System.out.println(ip.getHostName());
		}
	}


}

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net