ipv6从inet_pton得到网络序

        struct sockaddr_in6 sa;
        char str[INET6_ADDRSTRLEN];
        unsigned long nbuf[sizeof(struct in6_addr)];
        int i = inet_pton(AF_INET6, buf, (void *)&sa.sin6_addr);
        if (i > 0)
        {
            printf("0x%x%x%x%x\n", 
                            htonl(sa.sin6_addr.s6_addr32[0]),  
                            htonl(sa.sin6_addr.s6_addr32[1]), 
                            htonl(sa.sin6_addr.s6_addr32[2]), 
                            htonl(sa.sin6_addr.s6_addr32[3])); //#1
            inet_ntop(AF_INET6, &sa.sin6_addr, str, INET6_ADDRSTRLEN);
            printf("%s\n", str); // prints source ip //#2
        }
#1能够打印出ipv6的十六进制数
#2转回源IP

你可能感兴趣的:(ipv6从inet_pton得到网络序)