简介
netty是个好东西,如果不是机缘巧合之下去咬咬牙学了一下,可能就接触不到那么多厉害的技术了。
反正目标就是彻底搞明白Netty!!!
为此必须铺垫很多的基础知识,这里面知识用到最多的就是java的网络部分,java线程,java的io部分的知识了
因此我们会触类旁通地把所有netty要的知识点解释清楚
概览
这个小系列主要会解释一波java网络编程是怎么回事,在此先列出目录来
因为有很多知识点是穿插着的,必要的时候我会做相应的解释
- Internet地址
- URL
- HTTP相关概念
- 模拟请求
- socket编程(简单的bio编程)
- UDP编程
基本的网络概念
好,那么我们进入正题,既然要学会java的网络编程,持着尽量不漏掉必要的知识点,我们从最基础的东西说起
当然,网络要知道全部的东西是要三大本的~ 在此我们只会触碰那些必须的东西
Internet地址
网络地址简单的来说可以分为 IPv4与IPv6的地址,目前大体上IPv4的地址承担90%的业务请求
IPv4(a.b.c.d形式),abcd是0~255的数字。
IPv6(a.b.c.d.e.f形式),abcdef是16进制数
当然人类的记忆是很差劲的,数字的ip根本记不住。怎么办呢?通过DNS查询--只需要记住一个ip的别名,就可以查到这个别名对应的ip网址
InetAddress类
java中,InetAddress类是对IPv4与IPv6的封装,可以通过此类获得与ip相关的很多信息
获取实例
//你可以传入一个主机名得到一个InetAddress对象。
//还可以反向查找,例如,如果希望得到地址208.201.239.100的主机,可以向getByName()传入一个分四段地址来得到一个InetAddress对象
InetAddress.getByName();
//方法会为运行这个代码的主机返回一个InetAddress对象。
InetAddress.getLocalHost();
//如果你知道一个数字地址,可以由这个地址创建一个InetAddress对象,而不必使用InetAddress.getByAddress()与DNS交互。
InetAddress.getByAddress();
信息获取
//方法返回一个String,其中包含主机的名字,以及这个InetAddress对象表示的IP地址。只在不知道主机名时才会联系DNS。
public String getHostName()
//知道主机名时也会联系DNS,可能会替换原来缓存的主机名。
public String getCanonicalHostName()
//会以网络字节顺序将IP地址作为一个字节数返回。Java没有无符号字节这种基本数据类型,值大于127的字节会当作负数。需要将字节提升我int,并做适当的调整。
//之所以要查看IP地址的原始字节,一个原因是想要确定地址的类型,测试返回数组的字节数可以确定处理的IPv4还是IPv6地址。
public byte[] getAddress()
//方法返回一个字符串,其中包含分四段格式的IP地址。
public String getHostAddress()
地址类型
- isAnylocalAdress()
如果是通配地址,方法返回true。通配地址可以匹配本地系统中的任何地址,在IPv4中,通配地址是0.0.0.0。在IPv6中,通配地址是0:0:0:0:0:0:0:0。
- isLoopbackAdress()
如果是回送地址,方法返回true。回送地址直接在IP层连接同一台计算机,而不使用任务物理硬件。在IPv4中,这个地址是127.0.0.1。在IPv6中,这个回送地址是0:0:0:0:0:0:0:1。
- isLInkLocalAddress()
如果地址是一个IPv6本地链接地址,方法返回true。
- isSiteLocalAddress()
如果地址是一个IPv6本地网络地址,方法返回true。
- isMulticastAddress()
如果地址是一个组播地址,方法返回true。
- isMCGlobal()
如果地址是全球组播地址,方法返回true。
- isMCOrgLocal()
如果地址是一个组织范围组播地址,方法返回true。
- isMCSiteLocal()
如果地址是一个网站范围组播地址,方法返回true。
- isMCLinkLocal()
如果地址是一个子网范围组播地址,方法返回true。
- isMCNodeLocal()
如果地址是一个本地接口组播地址,方法返回true。
以下是上面提到的所有的点的测试代码集合
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws UnknownHostException {
showIntAddress(InetAddress.getByName("www.oreilly.com"));
showIntAddress(InetAddress.getByName("208.201.239.100"));
showIntAddress(InetAddress.getLocalHost());
showIntAddress(InetAddress.getByName("www.taobao.com"));
showIntAddress(InetAddress.getByName("183.136.135.225"));
showIntAddress(InetAddress.getByAddress(new byte[]{(byte)180,101,49,12}));
showIntAddress(InetAddress.getByAddress("www.baidu.com",new byte[]{(byte)180,101,49,12}));
}
public static void showIntAddress(InetAddress inetAddress){
System.out.println("inetAddress.getAddress() "+Arrays.toString(inetAddress.getAddress()));
System.out.println("inetAddress.getHostName() "+inetAddress.getHostName());
System.out.println("inetAddress.getHostAddress() "+inetAddress.getHostAddress());
System.out.println("inetAddress.getCanonicalHostName() "+inetAddress.getCanonicalHostName());
System.out.println("----");
System.out.println("inetAddress.isAnyLocalAddress() "+inetAddress.isAnyLocalAddress());//通配地址
System.out.println("inetAddress.isLoopbackAddress() "+inetAddress.isLoopbackAddress());//回送地址
System.out.println("inetAddress.isLinkLocalAddress() "+inetAddress.isLinkLocalAddress());//ip6的本地连接地址
System.out.println("inetAddress.isSiteLocalAddress() "+inetAddress.isSiteLocalAddress());//ip6的本地网站地址
System.out.println("inetAddress.isMulticastAddress() "+inetAddress.isMulticastAddress());//组播地址
System.out.println("inetAddress.isMCGlobal() "+inetAddress.isMCGlobal());//全球组播网址
System.out.println("inetAddress.isMCOrgLocal() "+inetAddress.isMCOrgLocal());//组织范围组播网址
System.out.println("inetAddress.isMCSiteLocal() "+inetAddress.isMCSiteLocal());//网站范围组播网址
System.out.println("inetAddress.isMCLinkLocal() "+inetAddress.isMCLinkLocal());//子网范围内组播网址
System.out.println("inetAddress.isMCNodeLocal() "+inetAddress.isMCNodeLocal());//本地接口组播地址
System.out.println("----------------------------");
}
}
测试可达性
InetAddress类有两个isReachable()方法,可以测试一个特定节点对当前主机是否可达。
NetWorkInterface类
获得实例
//表示有指定名字的网络接口
NetWorkInterface.getName(String name)
//方法返回一个NetworkInterface对象,表示与指定IP地址绑定的网络接口。
NetworkInterface.getByInetAddress(InetAddress address)
//方法返回一个枚举,列出本地主机上的所有网络接口。
NetworkInterface.getNetworkInterfaces()
//一个网络接口可以绑定多个IP地址。现在情况不太常见,但确实是存在的。
NetworkInterface.getInetAddresses()
//返回某个特定NetworkInterface对象的名,如eth0或lo。
NetworkInterface.getName()
//返回特定NetworkInterface的一个更友好的名字,如Ethernet Card 0。
NetworkInterface.getDisplayName()