Chinaunix

标题: IPV6 connect报错 [打印本页]

作者: seanking1987    时间: 2017-04-09 07:16
标题: IPV6 connect报错
代码如下:
  1. #include <time.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <net/if.h>
  8. #include <pthread.h>
  9. #include <sys/time.h>
  10. #include <arpa/inet.h>
  11. #include <sys/timeb.h>
  12. #include <sys/types.h>
  13. #include <sys/ioctl.h>
  14. #include <netinet/in.h>
  15. #include <sys/socket.h>

  16. extern int errno;

  17. void errorPrint()
  18. {
  19.         char *errMsg= strerror(errno);
  20.         printf("errno:%d,errorMsg:%s\n",errno,errMsg);
  21. }

  22. int main(void)
  23. {
  24.         struct sockaddr_in6 server;
  25.         struct sockaddr_in6 client;
  26.         int     sock;
  27.         int     val;
  28.         int rc=0;
  29.         char source_ip[40]={0};
  30.         char dest_ip[40]={0};
  31.         short src_port=4484;
  32.         short dest_port=2547;

  33.         memset(&server,0,sizeof(client));
  34.         memset(&client,0,sizeof(client));
  35.         server.sin6_family = AF_INET;
  36.         client.sin6_family = AF_INET;
  37.         server.sin6_port = htons(dest_port);
  38.         client.sin6_port = htons(src_port);
  39.         strcpy(source_ip,"2400:a480:aaaa:200::159");
  40.         strcpy(dest_ip,"2400:a480:aaaa:200::159");

  41.         rc=inet_pton(AF_INET6, source_ip,&client.sin6_addr);
  42.         if(rc!=1)
  43.         {
  44.                 printf("open_udp_socket_v6 error,inet_pton error!\n");
  45.                 return -1;
  46.         }
  47.         rc=inet_pton(AF_INET6,dest_ip,&server.sin6_addr);
  48.         if(rc!=1)
  49.         {
  50.                 printf("open_udp_socket_v6 error,inet_pton error!\n");
  51.                 return -1;
  52.         }

  53.         if((sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
  54.          {
  55.                 printf("open_udp_socket_v6 error(socket error)\n");
  56.                 return -1;
  57.          }

  58.         val = 1;
  59.        /*bind过程中本地地址可重复使用*/
  60.         if(setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val,sizeof(val)) < 0)
  61.          {
  62.                 close(sock);
  63.                 printf("open_udp_socket_v6 error(setsockopt error)\n");
  64.                 return -1;
  65.          }

  66.         if(bind(sock,(struct sockaddr*)&client,sizeof(client)) < 0)
  67.          {
  68.                 close(sock);
  69.                 printf("open_udp_socket_v6 error(bind error)\n");
  70.                 return -1;
  71.          }

  72.         if(connect(sock, (struct sockaddr *)&server, sizeof(server)) == -1)
  73.          {
  74.                 errorPrint();
  75.                 close(sock);
  76.                 printf("open_udp_socket_v6 error(connect error)\n");
  77.                 return -1;
  78.          }

  79.         return 0;
  80. }
复制代码
运行结果:
  1. errno:97,errorMsg:Address family not supported by protocol
  2. open_udp_socket_v6 error(connect error)
复制代码
求大神指导~

作者: seanking1987    时间: 2017-04-09 11:50
结贴,family错了,应该为AF_INET6  




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2