免费注册 查看新帖 |

Chinaunix

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

非阻塞和select [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-22 15:40 |只看该作者 |倒序浏览
想问一下,是不是设置了socket为非阻塞模式后,就不能再使用select了啊?
因为我自己写的程序中设置了socket为非阻塞模式后再利用select等待接收数据包时,每次都是超时,接收不到数据包。然后一怒之下把socket设置回普通模式,再把select去掉就可以了。不知道为什么。具体程序是这样的
/*sockopts = fcntl(thisint->sockInt,F_GETFL,0);
        if(sockopts<0)
        {
                printf("\neror getting socket options\n");
                return -3;
        }
        sockerr = fcntl(thisint->sockInt,F_SETFL,sockopts | O_NONBLOCK);
        if(sockerr<0)
        {
                printf("\nerror setting options for interface\n");
                return -3;
        }*/

这是设置为非阻塞模式
然后
while(1)
        {
          /* (void)sigfillset(&sigset_full);
           (void)sigdelset(&sigset_full,SIGINT);
           FD_ZERO(&read_set); FD_SET(p_fd,&read_set);
           timeout.tv_sec = 1;  
           timeout.tv_nsec = 0;
          
           //wait with all signals(except SIGINT) blocked.
         switch(pselect(p_fd+1,&read_set,NULL,NULL,&timeout,&sigset_full))
           {
           case -1://Normally, this case should not happen since sig_intr() never returns!
             printf("\nit is bad\n");
           case  0://time out
             switch(m_state)
             {
                     case 0:
                  printf("\ntime out ,not receive a packet\n");
                       if(++packetCount_SentFindServer>3)
                       {
                               printf("\nrestarting authentication\n");
                               goto beginAuthentication;
                       }
                       (void)SendFindServerPacket(intcur);
                       continue;//jump to next loop of while(1) to receive next packet
                     case 1:
                       if(++packetCount_SentName>3)
                       {
                               printf("\nrestarting authentication\n");
                               goto beginAuthentication;
                       }
                       (void)SendNamePacket(intcur,pkt_data);
                       continue;
                     case 2:
                       if(++packetCount_SentPassword>3)
                       {
                               printf("\nrestarting authentication\n");
                               goto beginAuthentication;
                       }
                       (void)SendPasswordPacket(intcur,pkt_data);
                       continue;
                     default:
                        printf("\nit is bad\n");
             }
           }*/
           printf("\nready to get packet\n");
           newsize = recvfrom(intcur->sockInt,pkt_data,1550,0,(struct sockaddr *)&intcur->sll,&socketlen);
这就接收不到包了。

论坛徽章:
0
2 [报告]
发表于 2008-09-23 14:03 |只看该作者
ms 是UDP 方式  那么在调用 recvfrom 之前 那个fd 会有状态改变吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP