主要由于ipv4拥有地址空间资源短缺的局限性,ipv6将会成为新一代的网络应用技术规范.所以下面介绍一下ipv6
1.Socket编程
增加<netinet/ip6.h>头文件
struct sockaddr {
unsigned short sa_family; /* address family, AF_xxx */
char sa_data[14]; /* 14 bytes of protocol address */
};
struct sockaddr_in6 {
sa_family_t sin6_family; /* AF_INET6 */
in_port_t sin6_port; /* transport layer port # */
uint32_t sin6_flowinfo; /* IPv6 traffic class & flow info */
struct in6_addr sin6_addr; /* IPv6 address */
uint32_t sin6_scope_id; /* set of interfaces for a scope */
};
struct in6_addr {
uint8_t s6_addr[16]; /* IPv6 address */
};
例子:
struct sockaddr_in6 local;
local.sin6_family = AF_INET6;
local.sin6_addr = in6addr_any;(相当于ipv4的INADDR_ANY);
local.sin6_port = port;
skt=socket( AF_INET6, SOCK_DGRAM, 0 );
bind( skt,(struct sockaddr *)&local, sizeof(local);
connect( skt, (struct sockaddr *)& local, sizeof(local) );
转换字符串到网络地址
int inet_pton(int af, const char *src, void *dst); 第一个参数af是地址族,转换后存在dst中
例:
struct in6_addr m_ip;
inet_pton(AF_INET6,”ff::1”, &m_ip);
转换网络地址成字符串 const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); 这个函数转换和上面的只是多了一个参数socklen_t cnt,他是所指向缓存区dst的大小,避免溢出