免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9716 | 回复: 7
打印 上一主题 下一主题

请教:Address family not supported by protocol [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-11 11:28 |只看该作者 |倒序浏览
想在客户端写一个reconnect,当服务器没启动时,能延长数秒,再连,但是出现了标题这个错误。
我的代码如下:

  1. //reconnect的定义
  2. int reconnect(int * sock_fd,const struct sockaddr *addr,socklen_t len)
  3. {
  4.     close(*sock_fd);
  5.     if (( *sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  6.     {
  7.         perror("socket");
  8.         return -1;
  9.     }
  10.     printf(" in reconnect sockfd is %d",*sock_fd);
  11.     if (connect(*sock_fd, (struct sockaddr *)&addr, len) == -1)
  12.     {
  13.         perror ("reconnect failed");
  14.         return -1;
  15.     }
  16.     else
  17.     return 1;
  18. }
  19. //reconnect 的使用,sockfd是int
  20. if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
  21.     {
  22.         perror("connect error ,now reconnected");
  23.         sleep(6);
  24.         if (reconnect(&sockfd,sizeof(struct sockaddr),sizeof(struct sockaddr)) == -1)
  25.         {
  26.             exit(1);
  27.         }     
  28.     }
复制代码

论坛徽章:
0
2 [报告]
发表于 2005-08-11 16:02 |只看该作者

请教:Address family not supported by protocol

if (reconnect(&sockfd,sizeof(struct sockaddr),sizeof(struct sockaddr)) == -1)
       {
           exit(1);
       }  
和函数原型对应的上吗?

论坛徽章:
0
3 [报告]
发表于 2005-08-11 16:08 |只看该作者

请教:Address family not supported by protocol

这个原因?难道要我用多一个参数来生成新的套接字,而不能使用原来的套接字?

论坛徽章:
0
4 [报告]
发表于 2005-08-11 16:38 |只看该作者

请教:Address family not supported by protocol

我是说第二个参数!

论坛徽章:
0
5 [报告]
发表于 2005-08-12 12:45 |只看该作者

请教:Address family not supported by protocol

if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
   {
       perror("connect error ,now reconnected";
       sleep(6);
       if (reconnect(&sockfd,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)) == -1)
       {
           exit(1);
       }     
   }

笔误。。。。。
还是同样这个错误

论坛徽章:
0
6 [报告]
发表于 2005-08-12 15:48 |只看该作者

请教:Address family not supported by protocol

[quote]原帖由 "locallocal"]这个原因?难道要我用多一个参数来生成新的套接字,而不能使用原来的套接字?[/quote 发表:

正解
connect失败后的套接字不能再用

论坛徽章:
0
7 [报告]
发表于 2014-09-11 14:42 |只看该作者
if (connect(*sock_fd, (struct sockaddr *)&addr, len) == -1)
第11行代码这里错了,(struct sockaddr *)addr
多了一个求地址符号

论坛徽章:
9
摩羯座
日期:2013-08-15 15:18:48狮子座
日期:2013-09-12 18:07:47金牛座
日期:2013-09-16 13:23:09辰龙
日期:2013-10-09 09:03:27白羊座
日期:2013-10-17 13:32:44子鼠
日期:2014-04-23 15:09:38戌狗
日期:2014-09-17 11:37:542015年亚洲杯之韩国
日期:2015-03-26 10:16:442015亚冠之武里南联
日期:2015-08-18 14:55:52
8 [报告]
发表于 2014-09-11 16:45 |只看该作者
回复 7# wan396


    真牛B,观察真仔细!
9年以后终于有了正确答案
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP