ipv6 socket编程实践

Table of Contents

  • 1. ipv6 socket编程实践
  • 2. 示例程序

ipv6 socket编程实践

  1. ip地址长度变化,按照ipv4 255.255.255.255的地址格式,最长为15,加上结束符16字节长度;ipv6
    的地址格式为XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX,最长为39,加上结束符为40字节长度。
  2. struct sockaddr变化为struct sockaddr_storage
    这个结构体变化在getpeername和getsockname两个函数中使用,虽然manual中给出的入参类型还是
    struct sockaddr,但是在ipv6中,需要使用struct sockaddr_storage
  3. struct sockaddr_in变化为struct sockaddr_in6
    在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
  4. inet_addr –> inet_pton
    TcpLinkCB[dwIndex].dwIPAddr = inet_addr(tmp); */ ipv4
    inet_pton(AF_INET6, tmp, &TcpLinkCB[dwIndex].in6peerIPAddr); /* ipv6

  5. 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
  6. bind函数入参变化
    bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr)) –>
    bind(sockfd, (struct sockaddr *) &my_addr, sizeof(struct sockaddr_in6)
  7. 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

  8. struct in6_addr可以使用IN6ADDR_ANY_INIT初始化
    struct in6_addr ia6 = IN6ADDR_ANY_INIT;

示例程序

  1. 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;  
      }
      
    • ipv6 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 ...\n");  
              printf("socket id :%d \n",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 .\n");  
              printf("local port : %d\n",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\n",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 ...\n");  
              printf("socket id :%d \n",mysocket);  
              printf("rmote ip : %s\n",REMOTEIP);  
              printf("remote port :%d \n",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\n",msg);  
          }  
      }
      
  2. ref: 示例程序来源

你可能感兴趣的:(c,ipv6,socket编程,示例程序)