1.IPv6概念
IPv6是InternetProtocol Version 6的缩写。IPv6是IETF(互联网工程任务组,InternetEngineering Task Force)设计的用于替代现行版本IP协议(IPv4)的下一代IP协议。
2.IPv6与IPv4相比具有的优势
a,IPv6具有更大的地址空间。IPv4中规定IP地址长度为32,即有2^32-1个地址;而IPv6中IP地址的长度为128,即有2^128-1个地址。
b,IPv6使用更小的路由表。IPv6的地址分配一开始就遵循聚类(Aggregation)的原则,这使得路由器能在路由表中用一条记录(Entry)表示一片子网,大大减小了路由器中路由表的长度,提高了路由器转发数据包的速度。
c,IPv6增加了增强的组播(Multicast)支持以及对流的支持(FlowControl),这使得网络上的多媒体应用有了长足发展的机会,为服务质量(QoS,Qualityof Service)控制提供了良好的网络平台。
d,IPv6加入了对自动配置(AutoConfiguration)的支持。这是对DHCP协议的改进和扩展,使得网络(尤其是局域网)的管理更加方便和快捷。
e,IPv6具有更高的安全性。在使用IPv6网络中用户可以对网络层的数据进行加密并对IP报文进行校验,极大的增强了网络的安全性。
3.IPv6的三种常规表示形式
1)十六进制:3FFE:FFFF:7654:FEDA:1245:BA98:3210:4562
2)压缩形式:例如,多路广播地址 FFED:0:0:0:0:BA98:3210:4562 的压缩形式为 FFED::BA98:3210:4562。单播地址3FFE:FFFF:0:0:8:800:20C4:0 的压缩形式为 3FFE:FFFF::8:800:20C4:0。环回地址0:0:0:0:0:0:0:1 的压缩形式为 ::1。未指定的地址 0:0:0:0:0:0:0:0 的压缩形式为 ::。
3)混合形式
此形式组合 IPv4 和 IPv6 地址。在此情况下,地址格式为 n:n:n:n:n:n:d.d.d.d,其中每个 n 都表示4个 IPv6 高序位 16 位地址元素之一的十六进制值,每个 d 都表示 IPv4 地址的十进制值。
4.IPv6 协议栈的安装及 IPv6 地址设置
a)windows下IPv6设置
(1) IPv6 协议栈的安装:在 开始 --> 运行 处执行 ipv6 install
(2) IPv6 地址设置:在 开始 --> 运行 处执行 netsh 进入系统网络参数设置环境,然后执行interface ipv6。画面显示:netsh interface ipv6>然后再执行:add address “本地连接” 2001:f80:754::3
(3) IPv6默认网关设置:在上述系统网络参数设置环境中执行:add route ::/0 “本地连接” 2001:f80:754::1 publish=yes
(4) IPv6地址删除:在 开始 --> 运行 处执行 netsh 进入系统网络参数设置环境,然后执行interface ipv6。画面显示:netsh interface ipv6>然后再执行:delete address “本地连接” 2001:f80:754::11 (注:必须以管理员身份运行CMD)
b)linux下IPv6的设置(linux内核自2.2.0开始都是支持IPv6内核这个模块的)
(1)手动添加IPv6地址和默认路由
#ifconfig eth0 add 2001:f80:754::2/64 (或如下)
#ifconfig eth0 inet6 add 2001:f80:754::2/64
#route -A inet6 add default gw 2001:f80:754::1/64 (配置时,没有修改默认漏油也没有出现过什么问题!)
//#ifconfig eth0 del 2001:f80:754::2/64 (delete)
(2)通过修改配置文件/etc/rc.local,配置文件下添加IPv6地址和默认路由
#vi /etc/rc.local (打开该文件)
#fconfig eth0 add 2001:f80:754::2/64 (IPv6地址)
#route -A inet6 add default gw 2001:f80:754::1/64 (默认路由)
5.使用IPv4 及 IPv6 地址socket通信的一些demo
为了编程的方便,一般我喜欢把socket通信有关的头文件都包含进来,省得麻烦,如下的头文件mynet.h
<span style="font-size:12px;">/*mynet.h*/
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/types.h>
#include <netinet/ip.h>
#include <arpa/ftp.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <net/if.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <ifaddrs.h>
#include <netdb.h> </span>
首先实现的是IPv4下的TCP连接,如下代码实现server:
/*tcpipv4serv.c*/
#include "mynet.h"
#define MyPort 10086
#define MyNum 20
int main(int argc,char* argv[]){
int mysock;
int clientfd;
int bytes = 0;
int len = sizeof(struct sockaddr_in);
struct sockaddr_in myaddr;
struct sockaddr_in cliaddr;
char *sendbuf = "hello client,i have recv msg";
char recvbuf[256];
char clientip[INET6_ADDRSTRLEN];
mysock = socket(AF_INET, SOCK_STREAM, 0);
if(mysock < 0){
printf("Error at socket().\n");
return -1;
}
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(MyPort);
if( bind(mysock, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0 ) {
printf("bind() failed. \n");
close(mysock);
return -1;
}
if(listen(mysock, MyNum) < 0) {
printf("Error listening on socket.\n");
close(mysock);
return -1;
}
printf("server listen...\n");
memset(&cliaddr, '0', sizeof(cliaddr));
for(;;){
clientfd = accept(mysock, (struct sockaddr*)&cliaddr, &len);
memset(recvbuf, '\0', sizeof(recvbuf));
if( bytes = recv(clientfd, recvbuf, sizeof(recvbuf), 0) < 0 ) {
printf("recv error!\n");
} else {
inet_ntop(AF_INET, &cliaddr.sin_addr, clientip, INET6_ADDRSTRLEN);
printf("C:%s->S: %s\n", clientip, recvbuf);
memset(recvbuf, '\0', sizeof(recvbuf));
if( bytes = send(clientfd, sendbuf, strlen(sendbuf)+1, 0) < 0 ) {
printf("recv error!\n");
} else {
printf("S->C:%s\n", sendbuf);
}
}
}
closesocket(mysock);
closesocket(clientfd);
return 0;
}
以下的代码是client端:
<span style="font-size:12px;">#include "mynet.h"
#define MyPort 10086
int main(int argc,char* argv[]){
struct sockaddr_in servaddr;
int mysock;
int bytes = 0;
int len = sizeof(struct sockaddr_in);
char mybuff[256];
if( argc != 3){
printf("usage: <exe> <serverip> <port>\n");
return -1;
}
mysock = socket(AF_INET, SOCK_STREAM, 0);
if(mysock < 0){
printf("Error at socket().\n");
return -1;
}
memset(&servaddr, 0, len);
servaddr.sin_family = AF_INET;
//servaddr.sin_addr.s_addr = htonl(atol(argv[1]));
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
servaddr.sin_port = htons(atoi(argv[2]));
if(connect(mysock,(struct sockaddr *) &servaddr, len) < 0){
printf("connect error!\n");
return -1;
}
memset(mybuff,0,sizeof(mybuff));
printf("Client Send:");
gets(mybuff);
bytes = send(mysock, mybuff, strlen(mybuff), 0);
if( bytes>0 ){
memset(mybuff,0,sizeof(mybuff));
}
bytes = recv(mysock, mybuff, sizeof(mybuff), 0);
if (bytes>0)
{
printf("client recv:%s\n", mybuff);
memset(mybuff,0,sizeof(mybuff));
}
}</span>
通过使用gcc 命令分别编译成tcpipv4serv和tcpipv4cli这两个可执行文件,如下图
图1-1 执行tcp server
图1-2执行tcp client
一下将贴出IPv6的server和client
<span style="font-size:12px;">/*tcpipv6serv.c*/
#include "mynet.h"
#define MyPort 10086
#define MyNum 20
int main(int argc,char* argv[]){
int mysock;
int clientfd;
int bytes = 0;
int len = sizeof(struct sockaddr_in);
struct sockaddr_in myaddr;
struct sockaddr_in cliaddr;
char *sendbuf = "hello client,i have recv msg";
char recvbuf[256];
char clientip[INET6_ADDRSTRLEN];
mysock = socket(AF_INET, SOCK_STREAM, 0);
if(mysock < 0){
printf("Error at socket().\n");
return -1;
}
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(MyPort);
if( bind(mysock, (struct sockaddr*)&myaddr, sizeof(myaddr)) < 0 ) {
printf("bind() failed. \n");
close(mysock);
return -1;
}
if(listen(mysock, MyNum) < 0) {
printf("Error listening on socket.\n");
close(mysock);
return -1;
}
printf("server listen...\n");
memset(&cliaddr, '0', sizeof(cliaddr));
for(;;){
clientfd = accept(mysock, (struct sockaddr*)&cliaddr, &len);
memset(recvbuf, '\0', sizeof(recvbuf));
if( bytes = recv(clientfd, recvbuf, sizeof(recvbuf), 0) < 0 ) {
printf("recv error!\n");
} else {
inet_ntop(AF_INET, &cliaddr.sin_addr, clientip, INET6_ADDRSTRLEN);
printf("C:%s->S: %s\n", clientip, recvbuf);
memset(recvbuf, '\0', sizeof(recvbuf));
if( bytes = send(clientfd, sendbuf, strlen(sendbuf)+1, 0) < 0 ) {
printf("recv error!\n");
} else {
printf("S->C:%s\n", sendbuf);
}
}
}
closesocket(mysock);
closesocket(clientfd);
return 0;
}</span>
<span style="font-size:12px;">/*tcpipv6cli.c*/
#include "mynet.h"
#define MyPort 10086
int main(int argc,char* argv[]){
struct sockaddr_in6 servaddr;
int mysock;
int bytes = 0;
int len = sizeof(struct sockaddr_in6);
char mybuff[256];
if( argc != 3){
printf("usage: <exe> <serverip> <port>\n");
return -1;
}
mysock = socket(AF_INET6, SOCK_STREAM, 0);
if(mysock < 0){
printf("Error at socket().\n");
return -1;
}
memset(&servaddr, 0, len);
servaddr.sin6_family = AF_INET6;
inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr);
servaddr.sin6_port = htons(atoi(argv[2]));
if(connect(mysock,(struct sockaddr *) &servaddr, len) < 0){
printf("connect error!\n");
return -1;
}
memset(mybuff,0,sizeof(mybuff));
printf("Client Send:");
gets(mybuff);
bytes = send(mysock, mybuff, strlen(mybuff), 0);
if( bytes>0 ){
memset(mybuff,0,sizeof(mybuff));
}
bytes = recv(mysock, mybuff, sizeof(mybuff), 0);
if (bytes>0)
{
printf("client recv:%s\n", mybuff);
memset(mybuff,0,sizeof(mybuff));
}
}</span>
<img src="https://img-blog.csdn.net/20140614171203453?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzg4OTc1Mw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />