ipv 6 socketプログラミング実践


Table of Contents
  • 1. ipv 6 socketプログラミング実践
  • 2. サンプルプログラム
  • ipv 6 socketプログラミング実践
  • ipアドレス長が変化し、ipv 4 255.255.255.255.255.255のアドレスフォーマットに従って、最長15で、終端16バイトの長さを加えた.ipv 6のアドレスフォーマットは、XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXXX:XXXXX:XXXXXで、最長39で、終了符を加えると40バイト長になります.
  • struct sockaddr変化struct sockaddr_storageという構造体の変化はgetpeernameとgetsocknameの2つの関数で使用されます.manualで与えられたパラメータタイプはstruct sockaddrですが、ipv 6ではstruct sockaddr_を使用する必要があります.storage
  • struct sockaddr_In struct sockaddrに変化in 6はinet_*次のように機能します.
  • sin_addr –> sin6_addr inet_ntop(AF_INET6, &sock_addr_in6.sin6_addr, peerAddrV6, sizeof(peerAddrV6)); */ipv6 peerAddr = (UINT8 *)inet_ntoa(sock_addr_in.sin_addr);/* ipv4
  • sin_port –> sin6_port fromV6.sin6_port = htons(TcpLinkCB[dwIndex].lPort); */ipv6 from.sin_port = htons(TcpLinkCB[dwIndex].lPort);/* ipv4

  • inet_addr –> inet_pton TcpLinkCB[dwIndex].dwIPAddr = inet_addr(tmp); */ipv4 inet_pton(AF_INET6, tmp, &TcpLinkCB[dwIndex].in6peerIPAddr);/* ipv6
  • socketインパラメータ変化AF_INET –> AF_INET6 skt = socket(AF_INET6, SOCK_STREAM, 0); */ipv6 skt = socket(AF_INET, SOCK_STREAM, 0);/* ipv4 also: PF_INET –> PF_INET6
  • bind関数パラメータ変化bind(sockfd,(struct sockaddr*)&my_addr, sizeof(struct sockaddr)) –> bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6)
  • INADDR_ANY –> in6addr_any struct sockaddr_in sa; struct sockaddr_in6 sa6; sa.sin_addr.s_addr = INADDR_ANY; */use my IPv4 address sa6.sin6_addr = in6addr_any;/* use my IPv6 address fromV6.sin6_family = AF_INET6; */ipv6 from.sin_family = AF_INET;/* ipv4
  • struct in6_addrはIN 6 ADDR_を使用できますANY_INIT初期化struct in 6_addr ia6 = IN6ADDR_ANY_INIT;

  • サンプルプログラム
  • ipv6
  • ipv6 tcp server
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #define MAXBUF 1024  
    int main(int argc, char **argv)  
    {  
        int sockfd, new_fd;  
        socklen_t len;  
    
        /* struct sockaddr_in my_addr, their_addr; */ // IPv4  
        struct sockaddr_in6 my_addr, their_addr; // IPv6  
    
        unsigned int myport, lisnum;  
        char buf[MAXBUF + 1];  
    
        if (argv[1])  
            myport = atoi(argv[1]);  
        else  
            myport = 7838;  
    
        if (argv[2])  
            lisnum = atoi(argv[2]);  
        else  
            lisnum = 2;  
    
        /* if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { */ // IPv4  
        if ((sockfd = socket(PF_INET6, SOCK_STREAM, 0)) == -1) { // IPv6  
            perror("socket");  
            exit(1);  
        } else  
            printf("socket created/n");  
    
        bzero(&my_addr, sizeof(my_addr));  
        /* my_addr.sin_family = PF_INET; */ // IPv4  
        my_addr.sin6_family = PF_INET6;    // IPv6  
        /* my_addr.sin_port = htons(myport); */ // IPv4  
        my_addr.sin6_port = htons(myport);   // IPv6  
        if (argv[3])  
            /* my_addr.sin_addr.s_addr = inet_addr(argv[3]); */ // IPv4  
            inet_pton(AF_INET6, argv[3], &my_addr.sin6_addr);  // IPv6  
        else  
            /* my_addr.sin_addr.s_addr = INADDR_ANY; */ // IPv4  
            my_addr.sin6_addr = in6addr_any;            // IPv6  
    
        /* if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) */ // IPv4  
        if (bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6))  // IPv6  
            == -1) {  
            perror("bind");  
            exit(1);  
        } else  
            printf("binded/n");  
    
        if (listen(sockfd, lisnum) == -1) {  
            perror("listen");  
            exit(1);  
        } else  
            printf("begin listen/n");  
    
        while (1) {  
            len = sizeof(struct sockaddr);  
            if ((new_fd =  
                 accept(sockfd, (struct sockaddr *) &their_addr,  
                        &len)) == -1) {  
                perror("accept");  
                exit(errno);  
            } else  
                printf("server: got connection from %s, port %d, socket %d/n",  
                       /* inet_ntoa(their_addr.sin_addr), */ // IPv4  
                       inet_ntop(AF_INET6, &their_addr.sin6_addr, buf, sizeof(buf)), // IPv6  
                       /* ntohs(their_addr.sin_port), new_fd); */ // IPv4  
                       their_addr.sin6_port, new_fd); // IPv6  
    
            /*                 */  
            bzero(buf, MAXBUF + 1);  
            strcpy(buf,  
                   "                      /n   new_fd   accept      socket   ,   sockfd    socket    ,  socket           /n");  
            /*         */  
            len = send(new_fd, buf, strlen(buf), 0);  
            if (len < 0) {  
                printf  
                    ("  '%s'    !     %d,     '%s'/n",  
                     buf, errno, strerror(errno));  
            } else  
                printf("  '%s'    ,    %d   !/n",  
                       buf, len);  
    
            bzero(buf, MAXBUF + 1);  
            /*          */  
            len = recv(new_fd, buf, MAXBUF, 0);  
            if (len > 0)  
                printf("      :'%s', %d      /n",  
                       buf, len);  
            else  
                printf  
                    ("      !     %d,     '%s'/n",  
                     errno, strerror(errno));  
            /*                 */  
        }  
    
        close(sockfd);  
        return 0;  
    }
    
  • ipv 6 tcp client
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #include   
    #define MAXBUF 1024  
    int main(int argc, char **argv)  
    {  
        int sockfd, len;  
        /* struct sockaddr_in dest; */ // IPv4  
        struct sockaddr_in6 dest;      // IPv6  
        char buffer[MAXBUF + 1];  
    
        if (argc != 3) {  
            printf  
                ("      !      :/n/t/t%s IP     /n/t  :/t%s 127.0.0.1 80/n         IP                MAXBUF       ",  
                 argv[0], argv[0]);  
            exit(0);  
        }  
        /*      socket    tcp    */  
        /* if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { */ // IPv4  
        if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {      // IPv6  
            perror("Socket");  
            exit(errno);  
        }  
        printf("socket created/n");  
    
        /*        (  )         */  
        bzero(&dest, sizeof(dest));  
        /* dest.sin_family = AF_INET; */  // IPv4  
        dest.sin6_family = AF_INET6;     // IPv6  
        /* dest.sin_port = htons(atoi(argv[2])); */ // IPv4  
        dest.sin6_port = htons(atoi(argv[2]));     // IPv6  
        /* if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0) { */ // IPv4  
        if ( inet_pton(AF_INET6, argv[1], &dest.sin6_addr) < 0 ) {                 // IPv6  
            perror(argv[1]);  
            exit(errno);  
        }  
        printf("address created/n");  
    
        /*       */  
        if (connect(sockfd, (struct sockaddr *) &dest, sizeof(dest)) != 0) {  
            perror("Connect ");  
            exit(errno);  
        }  
        printf("server connected/n");  
    
        /*           ,     MAXBUF     */  
        bzero(buffer, MAXBUF + 1);  
        /*           */  
        len = recv(sockfd, buffer, MAXBUF, 0);  
        if (len > 0)  
            printf("      :'%s', %d      /n",  
                   buffer, len);  
        else  
            printf  
                ("      !     %d,     '%s'/n",  
                 errno, strerror(errno));  
    
        bzero(buffer, MAXBUF + 1);  
        strcpy(buffer, "              /n");  
        /*         */  
        len = send(sockfd, buffer, strlen(buffer), 0);  
        if (len < 0)  
            printf  
                ("  '%s'    !     %d,     '%s'/n",  
                 buffer, errno, strerror(errno));  
        else  
            printf("  '%s'    ,    %d   !/n",  
                   buffer, len);  
    
        /*      */  
        close(sockfd);  
        return 0;  
    }
    
    コンパイルコマンド:gcc-o server./server.c gcc -o client ./client.c起動コマンド:./server port backlog ip_addr ./client ip_addr port
  • ipv6 udp server
    #include   
    #include   
    #include   
    #include   
    #include   
    #define LOCALPORT 8888  
    int main(int argc,char *argv[])  
    {  
        int mysocket,len;  
        int i=0;  
        struct sockaddr_in6 addr;  
        int addr_len;  
        char msg[200];  
        char buf[300];  
    
        if((mysocket=socket(AF_INET6,SOCK_DGRAM,0))<0)  
        {  
            perror("error:");  
            return(1);  
        }  
        else  
        {  
            printf("socket created ...
    "); printf("socket id :%d
    ",mysocket); } addr_len=sizeof(struct sockaddr_in6); bzero(&addr,sizeof(addr)); addr.sin6_family=AF_INET6; addr.sin6_port=htons(LOCALPORT); addr.sin6_addr=in6addr_any; if(bind(mysocket,(struct sockaddr *)&addr,sizeof(addr))<0) { perror("connect"); return(1); } else { printf("bink ok .
    "); printf("local port : %d
    ",LOCALPORT); } while(1) { bzero(msg,sizeof(msg)); len = recvfrom(mysocket,msg,sizeof(msg),0,(struct sockaddr *)&addr,(socklen_t*)&addr_len); printf("%d:",i); i++; inet_ntop(AF_INET6,&addr.sin6_addr,buf,sizeof(buf)); printf("message from ip %s",buf); printf("Received message : %s
    ",msg); if(sendto(mysocket,msg,len,0,(struct sockaddr *)&addr,addr_len)<0) { printf("error"); return(1); } } }
  • ipv6 udp client
    #include   
    #include   
    #include   
    #include   
    #include   
    #define REMOTEPORT 8888  
    #define REMOTEIP "::1"  
    int main(int argc,char *argv[])  
    {  
        int mysocket,len;  
        int i=0;  
        struct sockaddr_in6 addr;  
        int addr_len;  
        char msg[200];  
        if((mysocket=socket(AF_INET6,SOCK_DGRAM,0))< 0)  
        {  
            perror("error:");  
            return(1);  
        }  
        else  
        {  
            printf("socket created ...
    "); printf("socket id :%d
    ",mysocket); printf("rmote ip : %s
    ",REMOTEIP); printf("remote port :%d
    ",REMOTEPORT); } addr_len=sizeof(struct sockaddr_in6); bzero(&addr,sizeof(addr)); addr.sin6_family=AF_INET6; addr.sin6_port=htons(REMOTEPORT); inet_pton(AF_INET6,REMOTEIP,&addr.sin6_addr); while(1) { bzero(msg,sizeof(msg)); len=read(STDIN_FILENO,msg,sizeof(msg)); if(sendto(mysocket,msg,sizeof(msg),0,(struct sockaddr *)&addr,addr_len)<0) { printf("error"); return(1); } len=recvfrom(mysocket,msg,sizeof(msg),0,(struct sockaddr *)&addr,(socklen_t*)&addr_len); printf("%d:",i); i++; printf("Received message : %s
    ",msg); } }

  • ref:サンプルプログラムソース