关于InetAddress类
2015-10-10 10:57
155 查看
前言:
1> Internet上的主机有两种方式表示地址:
域名(hostName):www.atguigu.com
IP 地址(hostAddress):202.108.35.210
2> InetAddress类主要表示IP地址,两个子类:Inet4Address、Inet6Address。
3> InetAddress 类对象含有一个 Internet 主机地址的域名和IP地址:www.atguigu.com 和 202.108.35.210。
4> InetAddress类没有提供公共的构造器,而是提供了如下两个静态方法来获取InetAddress实例
5> InetAddress提供了如下几个常用的方法
代码示例如下:
运行的结果如下:
www.atguigu.com/42.121.6.2
www.atguigu.com
42.121.6.2
PC-20150411BHRQ/192.168.182.1
PC-20150411BHRQ
192.168.182.1
1> Internet上的主机有两种方式表示地址:
域名(hostName):www.atguigu.com
IP 地址(hostAddress):202.108.35.210
2> InetAddress类主要表示IP地址,两个子类:Inet4Address、Inet6Address。
3> InetAddress 类对象含有一个 Internet 主机地址的域名和IP地址:www.atguigu.com 和 202.108.35.210。
4> InetAddress类没有提供公共的构造器,而是提供了如下两个静态方法来获取InetAddress实例
5> InetAddress提供了如下几个常用的方法
代码示例如下:
package com.atguigu.java1; import java.net.InetAddress; import java.net.UnknownHostException; /* * 网络通信的第一个要素:IP地址。通过IP地址,唯一的定位互联网上一台主机 * InetAddress:位于java.net包下 * 1.InetAddress用来代表IP地址。一个InetAdress的对象就代表着一个IP地址 * 2.如何创建InetAddress的对象:getByName(String host) * 3.getHostName(): 获取IP地址对应的域名 * getHostAddress():获取IP地址 */ public class TestInetAddress { public static void main(String[] args) throws Exception { //创建一个InetAddress对象:getByName() InetAddress inet = InetAddress.getByName("www.atguigu.com"); //inet = InetAddress.getByName("42.121.6.2"); System.out.println(inet); //两个方法 System.out.println(inet.getHostName()); System.out.println(inet.getHostAddress()); //获取本机的IP:getLocalHost() InetAddress inet1 = InetAddress.getLocalHost(); System.out.println(inet1); System.out.println(inet1.getHostName()); System.out.println(inet1.getHostAddress()); } }
运行的结果如下:
www.atguigu.com/42.121.6.2
www.atguigu.com
42.121.6.2
PC-20150411BHRQ/192.168.182.1
PC-20150411BHRQ
192.168.182.1
相关文章推荐
- 关于Java的反射
- H264中的SPS、PPS提取与作用
- 关于Java的访问控制修饰符
- LAMP扩展
- 关于Java的继承
- 在Java中,关于final关键字
- 在Java中,关于static关键字
- 在Java中,关于abstract关键字
- 在Java中,关于接口的概念
- 在Java中,关于this和super
- 在Java中,关于package和import
- 内部类和匿名内部类
- apache tomcat nginx的区别
- Struts在控制器内的三种基本接收参数的方式
- oracle分析函数——over
- SQLite数据库
- activity的task总结
- 集合 Properties 的 简单例子(Spring)
- 关于Java的异常处理
- 在Java中,关于线程的通信