免费注册 查看新帖 |

Chinaunix

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

[C] 关于select返回值的问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-01 14:13 |只看该作者 |倒序浏览
在服务器侧,使用select检查新建的socket是否可读写,超时时间设置为5秒,
但由于采用了多线程,如果在接收线程进入到select后,其他线程中shutdown和close掉这个建立的socket后,
select没有立即返回,请问是怎么回事?
谢谢了

论坛徽章:
0
2 [报告]
发表于 2011-08-01 16:15 |只看该作者
是windows的程序吧。window socket的实现就是这样的。

论坛徽章:
0
3 [报告]
发表于 2011-08-01 19:02 |只看该作者
  1.                         int resultS = ::select(fd + 1, &fdSet, 0, 0, &tv);//使用select()防止拒绝服务型攻击
  2.                        
  3.                         if(resultS == 0)
  4.                         {
  5.                                 //cout <<"1111"<<endl;
  6.                                
  7.                                
  8.                                 if (lenLeft > 0)
  9.                                 {//部分接收, 超时
  10.                                         ret_code = COMMON_BSAE_ERROR_NO - 121;
  11.                                         //printf("In CTransCommonSocket::RecvFromNet, select(%d) timeout error number = %d...\n", fd, ret_code);
  12.                                         cout <<"In CTransCommonSocket::RecvFromNet, select("<<fd<<") timeout error number = " <<ret_code<<"..."<<endl;
  13.                                        

  14.                                        
  15.                
  16.                                         return ret_code;
  17.                                 }
  18.                                 else
  19.                                 {
  20.                                         //cout <<"222"<<endl;
  21.                                         //如果强制无限等待,不进行超时返回
  22.                                         if( timeout != WAITINFINE )                        //一直等待标志
  23.                                         {
  24.                                                
  25.                                                 cout <<"timeout"<<timeout<<endl;
  26.                                                 return 0;  //未接收, timeout
  27.                                         }
  28.                                         else
  29.                                         {
  30.                                                 //cout <<"In CTransCommonSocket::RecvFromNet, timeout is waitting..."<<endl;
  31.                                                 continue;
  32.                                         }
  33.                                        
  34.                                        
  35.                                 }
  36.                         }                       
  37.                         else if(resultS == -1)
  38.                         {
  39.                                 if(IsInterrupted())
  40.                                     continue;
  41.                                 int err=GetError();
  42.                                
  43.                                 ret_code = COMMON_BSAE_ERROR_NO - 122;
  44.                                 //printf("In CTransCommonSocket::RecvFromNet, select() error number = %d...\n", ret_code);
  45.                                 cout <<"In CTransCommonSocket::RecvFromNet, select("<<fd<<") error number = " <<ret_code<<",GetError():"<<GetError()<<"..."<<endl;
  46.                                 return ret_code;
  47.                         }

  48. int result = ::recv(fd_, (char*)(buf + lenLeft), sizeOfPacket, 0);
  49.                 if(result == 0)//false, connection closed
  50.                 {
  51.                         int err=GetError();
  52.                        
  53.                         ret_code = COMMON_BSAE_ERROR_NO - 123;
  54.                         //printf("CTransCommonSocket::RecvFromNet, recv(%d) error number = %d...\n", fd_, ret_code);
  55.                         return ret_code;
  56.                 }
  57.                 else if(result == -1)
  58.                 {
  59.                         if(NoBufs() && sizeOfPacket > 1024)
  60.                         {
  61.                                 sizeOfPacket /= 2;
  62.                                 continue;
  63.                         }
  64.                        
  65.                         if(IsInterrupted())
  66.                                 continue;

  67.                         if(fd_ == -1 || ConnectionDown())
  68.                         {//false, connection loss
  69.                                
  70.                                 int err=GetError();
  71.                                
  72.                        
  73.                                 ret_code = COMMON_BSAE_ERROR_NO - 124;
  74.                                 printf("In CTransCommonSocket::RecvFromNet, recv() error number = %d, fd = %d...\n", ret_code, fd_);
  75.                                 return ret_code;
  76.                         }
  77.                         if(WouldBlock())
  78.                                 continue;
  79.                        
  80.                         int err=GetError();
  81.                        
  82.                                
  83.                         ret_code = COMMON_BSAE_ERROR_NO - 125;
  84.                         printf("In CTransCommonSocket::RecvFromNet, recv() error number = %d...\n", ret_code);
  85.                         return ret_code;
  86.                 }
复制代码
不是windows的,标准C下面的

论坛徽章:
0
4 [报告]
发表于 2011-08-02 00:27 |只看该作者
你测试过 linux 下的确是这样子?

论坛徽章:
0
5 [报告]
发表于 2011-08-02 15:27 |只看该作者
确定,select一直都是超时才能退出
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP