java网络---查找Internet
连接到Internet的设备称为节点,计算机节点称为host。
为了区别每一台连接互联网的计算机,就有了Internet Protocol地址的概念。
IPV4 & IPV6
我们以前默认的是IPV4. 也就类似于192.168.0.1 这样的地址
但是,由于连接到网络的设备急剧增加,IPv4 会不够。所以有了IPV6
这里4 和6 是协议的版本,不是ip地址的个数。
IPV6 是以8个字段表示的地址。
IP地址对于计算机很重要,但是对于个人来说,很难记住那么多的数字。尤其是当有很多的网址的时候。
于是有了域名系统,也就是我们打入“www.baidu.com” 他会连接到115.239.211.112这样的地址。
一:InetAddress
InetAddress的构造函数不是公开的(public),所以需要通过它提供的静态方法来获取,有以下的方法: static InetAddress[] getAllByName(String host) static InetAddress getByAddress(byte[] addr) static InetAddress getByAddress(String host,byte[] addr) static InetAddress getByName(String host) static InetAddress getLocalHost()
InetAddress address=InetAddress.getByName("www.baidu.com");
@Override public void start() { try { InetAddress address = InetAddress.getByName("www.oreilly.com"); TraceLog.i(address.toString()); }catch (UnknownHostException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } }
12-23 16:45:10.786 4286-4909/com.joyfulmath.sample.javanetwork I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
12-23 16:45:10.791 4286-4909/com.joyfulmath.sample.javanetwork I/System.out: propertyValue:true
12-23 16:45:10.792 4286-4909/com.joyfulmath.sample.javanetwork I/INetAddressSample: start: www.oreilly.com/104.91.236.215 [at (INetAddressSample.java:18)]
地址类型:
/** * Returns whether this address is a loopback address or not. * * <p>Valid IPv4 loopback addresses have the prefix {@code 127/8}. * * <p>The only valid IPv6 loopback address is {@code ::1}. */ public boolean isLoopbackAddress() { return false; }
是否为本地地址,默认一般为127.0.0.1 这样的地址。
可达性:
就是让应用程序测试,当前主机能否达到某个连接。
private void test3() throws IOException { InetAddress address = InetAddress.getByName("www.oreilly.com"); TraceLog.i(String.valueOf(address.isReachable(1000))); }
12-24 09:50:40.465 31459-31726/com.joyfulmath.sample.javanetwork I/INetAddressSample: test3: false [at (INetAddressSample.java:49)]
由于全球网络内部很多都有防火墙之类的东西,所以结果很多情况下都是false。
equals方法:
判断方式只是ip地址,不考虑主机。
二:NetworkInterface
InetAddress是ip地址的高级表示,NetworkInterface表示的是物理接口或者虚拟地址。
private void test4() throws SocketException { Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); while (netInterfaces.hasMoreElements()) { NetworkInterface nif = netInterfaces.nextElement(); Enumeration<InetAddress> iparray = nif.getInetAddresses(); while (iparray.hasMoreElements()) { InetAddress ip = iparray.nextElement(); TraceLog.i(ip.getHostAddress()); } } }
12-24 10:06:28.250 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test1: www.oreilly.com/23.44.132.94 [at (INetAddressSample.java:36)] 12-24 10:06:28.250 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test1: www.oreilly.com [at (INetAddressSample.java:38)] 12-24 10:06:28.268 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test2: getLocalHost localhost/127.0.0.1 [at (INetAddressSample.java:44)] 12-24 10:06:28.268 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test2: true [at (INetAddressSample.java:46)] 12-24 10:06:29.271 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test3: false [at (INetAddressSample.java:53)] 12-24 10:06:29.300 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test4: fe80::6a3e:34ff:fe33:3561%wlan0 [at (INetAddressSample.java:64)] 12-24 10:06:29.301 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test4: 10.59.92.19 [at (INetAddressSample.java:64)] 12-24 10:06:29.301 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test4: ::1%1 [at (INetAddressSample.java:64)] 12-24 10:06:29.302 9472-10121/com.joyfulmath.sample.javanetwork I/INetAddressSample: test4: 127.0.0.1 [at (INetAddressSample.java:64)]
参考:
posted on 2015-12-24 10:08 Joyfulmath 阅读(495) 评论(0) 编辑 收藏 举报