IPV6 Socket 编程

原文:http://blog.csdn.net/caspiansea/article/details/29779655

写了一个 IPv6的服务端和客户端的程序,功能就是服务端重复一下客户端的输入(根据 UNP代码改的)。

代码如下:

server6.c:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <sys/socket.h>  
  2. #include <sys/types.h>  
  3. #include <netinet/in.h>  
  4. #include <errno.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7.   
  8. #define SERV_PORT        9877  
  9. #define LISTENQ         1024  
  10. extern int echo(int sockfd);  
  11. int main(int argc, char ** argv)  
  12. {  
  13.     int listenfd, connfd;  
  14.     struct sockaddr_in6 servaddr, cliaddr;  
  15.   
  16.     listenfd = socket(AF_INET6, SOCK_STREAM, 0);  
  17.       
  18.     if(listenfd == 0)  
  19.     {  
  20.         perror("socket create error");  
  21.         exit(1);  
  22.     }  
  23.   
  24.     bzero(&servaddr, sizeof(servaddr));  
  25.     servaddr.sin6_family = AF_INET6;  
  26.     servaddr.sin6_addr = in6addr_any;  
  27.     servaddr.sin6_port = htons(SERV_PORT);  
  28.   
  29.     if(bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)  
  30.     {  
  31.         perror("bind error");  
  32.         exit(2);  
  33.     }  
  34.   
  35.     if(listen(listenfd, LISTENQ) == -1)  
  36.     {  
  37.         perror("listen error");  
  38.         exit(3);  
  39.     }  
  40.   
  41.   
  42.     for(; ;)  
  43.     {  
  44.         socklen_t clien = sizeof(struct sockaddr);  
  45.         connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clien);  
  46.         if(connfd == -1)  
  47.         {  
  48.             perror("accept error");  
  49.             exit(4);  
  50.         }   
  51.   
  52.         pid_t childpid = fork();  
  53.         if(childpid == -1)  
  54.         {  
  55.             perror("fork error");  
  56.             exit(5);  
  57.         }  
  58.         else if(childpid == 0)  
  59.         {  
  60.             close(listenfd);  
  61.             echo(connfd);  
  62.             exit(0);      
  63.         }  
  64.         close(connfd);  
  65.     }  
  66. }  

client6.c:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/socket.h>  
  3. #include <netinet/in.h>  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6. #include <string.h>  
  7.   
  8. #define SERV_PORT  9877  
  9.   
  10. int main(int argc, char **argv)  
  11. {  
  12.     int sockfd;  
  13.     struct sockaddr_in6 servaddr;  
  14.       
  15.     if(argc != 2)  
  16.     {  
  17.         printf("usage: %s server address\n", argv[0]);  
  18.         exit(0);  
  19.     }  
  20.   
  21.       
  22.     sockfd = socket(AF_INET6, SOCK_STREAM, 0);  
  23.       
  24.     if(sockfd == -1)  
  25.     {  
  26.         perror("socket error");  
  27.         exit(1);  
  28.     }  
  29.   
  30.     bzero(&servaddr, sizeof(servaddr));  
  31.     servaddr.sin6_family = AF_INET6;  
  32.     servaddr.sin6_port = htons(SERV_PORT);  
  33.   
  34.     int n = inet_pton(AF_INET6, argv[1], &servaddr.sin6_addr);  
  35.     if(n == 0)  
  36.     {  
  37.         fprintf(stderr, "not in presentation format\n");  
  38.         exit(2);  
  39.     }  
  40.     else if(n == -1)  
  41.     {  
  42.         perror("inet_pton error");  
  43.         exit(3);  
  44.     }  
  45.   
  46.     printf("sizeof(servaddr) = %d\n"sizeof(servaddr));  
  47.     if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(struct sockaddr_in6)) == -1)  
  48.     {  
  49.         perror("connect error:");  
  50.         exit(4);  
  51.     }  
  52.   
  53.     str_cli(stdin, sockfd);  
  54.     return 0;  
  55. }  

但是客户端在使用 IPv6地址连接服务器程序的时候,出现错误:
[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. t#./client6 fe80::5054:ff:fe12:3456  
  2. connect error:: Invalid argument  
fe80::5054:ff:fe12:3456 为 IPv6地址 (link-local).

但是加上一个其它类型的地址 (如global 的),然后,  client就能连上了。

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. sudo ip addr add 2001:470:1f01:f52b::2/64 dev wlan0  
  2. or  
  3. <pre name="code" class="plain">sudo ifconfig wlan0 inet6 add 2001:470:1f01:f52b::2/64  

 
 
[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. wlan0     Link encap:Ethernet  HWaddr ec:55:f9:bf:7c:16    
  2.           inet addr:192.168.1.102  Bcast:192.168.1.255  Mask:255.255.255.0  
  3.           inet6 addr: fe80::ee55:f9ff:febf:7c16/64 Scope:Link  
  4.           inet6 addr: 2001:470:1f01:f52b::2/64 Scope:Global  
  5.           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
  6.           RX packets:2445788 errors:0 dropped:0 overruns:0 frame:0  
  7.           TX packets:1667005 errors:0 dropped:0 overruns:0 carrier:0  
  8.           collisions:0 txqueuelen:1000   
  9.           RX bytes:2493540196 (2.4 GB)  TX bytes:284771192 (284.7 MB)  

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. ./client6 2001:470:1f01:f52b::2  
  2. sizeof(servaddr) = 28  
  3. 1  
  4. 1  

网上有资料说
[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1.  You got a bit more to cover to get up and flying with IPv6. Like you  
  2. should NOT be using Link Local addresses for anything in user space (they're  
  3. primarily used in the kernel and lower level protocol stuff for things like  
  4. neighbor discovery and router solicitation. You can use them with certain  
  5. apps, like ping6, IF you know what you are doing. But not with apps which  
  6. don't understand what you are doing.   

References:

http://www.derkeiler.com/Mailing-Lists/securityfocus/Secure_Shell/2004-01/0013.html
http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/index.html
http://www.bieringer.de/linux/IPv6/IPv6-HOWTO/IPv6-HOWTO-6.html

https://wiki.ubuntu.com/IPv6

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值