《java入门第一季》之网络编程初探

由于在写有关javaweb的博客,在写到web服务器的时候需要回顾网络编程的知识,提前把网络编程放在前面写。

直接上代码解释:

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

/*
 * 如果一个类没有构造方法:
 * A:成员全部是静态的(Math,Arrays,Collections)
 * B:单例设计模式(Runtime)
 * C:类中有静态方法返回该类的对象(InetAddress)
 * 		class Demo {
 * 			private Demo(){}
 * 
 * 			public static Demo getXxx() {
 * 				return new Demo();
 * 			}
 * 		}
 * 
 * 看InetAddress的成员方法:
 * public static InetAddress getByName(String host):根据主机名或者IP地址的字符串表示得到IP地址对象
 */
public class InetAddressDemo {
	public static void main(String[] args) throws UnknownHostException {
		// public static InetAddress getByName(String host)
		// InetAddress address = InetAddress.getByName("ydl-PC");
		
		InetAddress address = InetAddress.getByName("192.168.1.105");

		// 获取两个东西:主机名,IP地址
		// public String getHostName()
		String name = address.getHostName();
		// public String getHostAddress()
		String ip = address.getHostAddress();
		System.out.println(name + "---" + ip);
	}
}

输出结果:ydl-PC---192.168.1.105

原文地址:https://www.cnblogs.com/wanghang/p/6299736.html