您的位置:首页 > 理论基础 > 计算机网络

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

2016-05-28 20:03 483 查看
由于在写有关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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: