java 网络编程,
分享于 点击 25722 次 点评:120
java 网络编程,
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*
* 网络编程
*
* java.net包
*
*
* 网络 通信的第一要素 IP地址 --- 通过ip唯一定位一台互联网机器
*
* InetAddress类 用来代表ip地址
*
* 一个InetAddress对象就代表 一个ip地址
*
* 创建一个 InetAddress对象, InetAddress.getByName(String host);
*
*
*
*
*
* getHostName(); 获取ip 地址对应的 域名
*
* getHostAddress(); 获取 ip地址
*
* InetAddress.getLocalHost() 获取本地ip
*
*
*
* @author Administrator
*
*/
public class TestInetAddress {
public static void main(String[] args) throws UnknownHostException {
// 通过域名或者ip地址 获取 一个 InetAddress
InetAddress na = InetAddress.getByName("www.baidu.com");
//
System.out.println(na);
System.out.println(na.getHostAddress());
System.out.println(na.getHostName());
System.out.println();
// 获取本机的ip
InetAddress ia = InetAddress.getLocalHost();
System.out.println(ia);
// 获取网络名
System.out.println(ia.getHostName());
// 获取ip地址
System.out.println(ia.getHostAddress());
}
}
相关文章
- 暂无相关文章
用户点评