免费注册 查看新帖 |

Chinaunix

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

非阻塞发送是,select超时,怎么办? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-03 14:55 |只看该作者 |倒序浏览
本帖最后由 urapple 于 2011-03-03 15:00 编辑

非阻塞发送,我的select超时设置为5秒,居然有时还出现很多超时信息。该如何避免?
  1.         boost::mutex::scoped_lock scoped_lock(this->send_mutex)  ;
  2.         {
  3.                 while (bytes_to_write_total > 0)
  4.                 {
  5.                         //超时由1秒增加为5秒. 2010/9/25.
  6.                         if ((selret=SendableTimeOut(5, 0))>0)//only one, so we dont FD_ISSET.
  7.                         {
  8.                                 bytes_write = Send(ptemp, bytes_to_write_total);
  9.                                 if (bytes_write < 0)
  10.                                 {
  11.         #if defined(WIN32) || defined(WINNT)
  12.                                         if((iErrorRet=WSAGetLastError()) == WSAEWOULDBLOCK)
  13.         #else
  14.                                         if (EAGAIN == errno || EINTR == errno)
  15.         #endif
  16.                                         {
  17.                                                 //在select 检查能写的情况下,又进入这里,本身就是矛盾的,异常情况.2010/9/17.
  18.                                                 cout<<"[send]: bytes_write<0, WSAEWOULDBLOCK but select ok!"<<endl;
  19.                                                 #if !defined(WIN32) && !defined(WINNT)
  20.                                                 iErrorRet = errno;
  21.                                                 #endif
  22.                                                 cx ++;
  23.                                                 if (cx>SEND_RETRY_COUNT)
  24.                                                 {
  25.                                                         error = 2;
  26.                                                         //printf("[send]: retry <0 error!\n");
  27.                                                         break;
  28.                                                 }
  29.                                         }
  30.                                         else
  31.                                         {
  32.                                                 #if !defined(WIN32) && !defined(WINNT)
  33.                                                 iErrorRet = errno;
  34.                                                 #endif
  35.                                                 error = 1;
  36.                                                 //printf("[send]: <0 error!\n");
  37.                                                 break;
  38.                                         }
  39.                                 }
  40.                                 else
  41.                                 {
  42.                                         bytes_to_write_total -= bytes_write;
  43.                                         bytes_have_written_total += bytes_write;
  44.                                         ptemp += bytes_have_written_total;
  45.                                 }                        
  46.                         }
  47.                         else if(selret==0)
  48.                         {
  49.                                 //超时不作为出错处理. 2010/9/25.
  50.                                 cout<<"[send]: select timeout!"<<this->dev_type<<":"<<this->list_id<<endl;
  51.                         }
  52.                         else
  53.                         {
  54.                                 error = 1;
  55.                                 cout<<"[send]: select error!"<<endl;
  56.                                 break;
  57.                         }
  58.                 }
  59.         }
  60.         if (error==1)//net break,exit.
  61.         {
  62.                 cout<<"send pack fail! "<<iErrorRet<<":"<<this->dev_type<<endl;
  63.                 string swip;
  64.                 unsigned int port;
  65.                 GetPeerName(swip,port);
  66.                 cout<<"sock_type="<<m_type<<" ip="<<swip<<" port="<<port<<endl;
  67.                
  68.                 m_needExit=true;
  69.         }
  70.         else if (error==2)
  71.         {
  72.                 cout<<"retry too many! give it up! exit!"<<endl;
  73.                 m_needExit=true;//2010/9/17.
  74.         }
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-03-03 14:56 |只看该作者
  1. int CMySocket::SendableTimeOut(int second, int microsecond)
  2. {

  3.         struct timeval timeout;
  4.         fd_set writeSocketSet;
  5.        
  6.         FD_ZERO( &writeSocketSet );
  7.         FD_SET(m_hSocket, &writeSocketSet );
  8.         timeout.tv_sec = second;
  9.         timeout.tv_usec = microsecond;
  10.         return (select( m_hSocket+1, NULL, &writeSocketSet, NULL, &timeout ));
  11. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-03-03 14:57 |只看该作者
  1. #define SEND_RETRY_COUNT 5
复制代码

论坛徽章:
0
4 [报告]
发表于 2011-03-03 15:05 |只看该作者
是否因为对方接收不及时或者没有接收数据导致发送不出去?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP