实现数据包超时c ++

| 对于计算机网络项目,我们正在用c ++编写具有不同窗口协议的ftp服务器。我们在实现工作超时功能方面遇到问题。其主旨是将时间戳记设置为发送数据包的时间,并且如果在一定时间内(例如2.5毫秒)未恢复“ ack”,则重新发送数据包。当前,我们使用
clock_gettime()
函数获取数据包的当前时间和时间戳。 这是while循环,我们遇到了问题:
while(packet_sync_array[prev].recieved ==0 && t==0)
{
    clock_gettime(CLOCK_PROCESS_CPUTIME_ID,&timeout2);
    currtime = timeout2.tv_nsec;
    cout << \"Is currtime: \"<<currtime<< \" - ptimeout \" << ptimeout 
         << \" = \" << (currtime - ptimeout) << \" > \" 
         << (connection_parameters->timeoutInterval)*1000 << \"?\" << endl;

    if((currtime - ptimeout)>((connection_parameters->timeoutInterval)*1000))
    {
        t = 1;
        cout << \"Prev PACKET TIMEDOUT\" << endl;
        cout << \"Is currtime: \"<<currtime<< \" - ptimeout \" << ptimeout 
             << \" = \" << (currtime - ptimeout) << \" > \" 
             << (connection_parameters->timeoutInterval)*1000 << \"?\" << endl;
    }
}  
其中“ 2”是发送数据包的时间,“ 3”是超时间隔。问题在于,由于
ptimeout
是时间的长整数表示,因此有时它是一个非常大的值(例如999715992)。这意味着它将永远无法判断是否发生了超时,因为当前时间(以纳秒为单位)将在值变得足够大之前恢复为0。 其他人有没有用c ++处理过这些计时问题并有可能的解决方案? 谢谢! 编辑: 感谢您的快速回复!我能够弄清楚一些东西。修改为while循环,以检查超时+时间戳是否大于允许的长整数间隔,让我看看在比较之前clock_gettime是否会退回零。知道这一点后,我检查了当前时间是否>(超时间隔-(最大long int val-时间戳))。这允许最多1秒钟的超时,对于此问题而言,这应该足够了。如果有人认为他们有更好的解决方案,请告诉我!谢谢! 这是那些有兴趣的代码:
if(((999999998-ptimeout)< (connection_parameters->timeoutInterval)*1000)&&(currtime - ptimeout)<0){
          if(currtime > (((connection_parameters->timeoutInterval)*1000)-(999999998-ptimeout))){
              t = 1;
              cout << \"this wrapped since \" << currtime << \" + \" << (connection_parameters->timeoutInterval)*1000 << \"is bigger than 999999999 and then timed out\" << endl;
              cout << \"curr time \" << currtime << \"is bigger than\" << endl;
              cout << (connection_parameters->timeoutInterval)*1000 << endl;
              cout << \"-\" << endl;
              cout << (999999998-ptimeout) << endl;
              cout << \"---------------------------\" << endl;
              cout << (((connection_parameters->timeoutInterval)*1000)-(999999998-ptimeout)) << endl;
              cout << \"Prev PACKET TIMEDOUT\" << endl;
              cout << \"Is currtime: \"<<currtime<< \" - ptimeout \" << ptimeout << \" = \" << (currtime - ptimeout) << \" > \" << (connection_parameters->timeoutInterval)*1000 << \"?\" << endl;
          }
      }
      else if((currtime - ptimeout)>((connection_parameters->timeoutInterval)*1000)){
       t = 1;
       cout << \"Prev PACKET TIMEDOUT\" << endl;
       cout << \"Is currtime: \"<<currtime<< \" - ptimeout \" << ptimeout << \" = \" << (currtime - ptimeout) << \" > \" << (connection_parameters->timeoutInterval)*1000 << \"?\" << endl;
      }
    
已邀请:
您可能需要更改代码以使其:
// time in microseconds
currtime = timeout2.tv_sec * 1000000 + timeout2.tv_nsec / 1000;
只要确保您没有任何整数溢出!例如,显式转换为64位整数可能是一个好主意。 另外,您很可能希望超时至少大于2.5毫秒一个数量级,甚至更多。例如,您在操作系统中的时间片很可能约为17毫秒,更不用说网络延迟了。     
您需要查看
clock_gettime
所填充的
timespec
对象的
tv_sec
tv_nsec
组件。您的
ptimeout
字段应为
timespec
而不是整数。您将必须编写函数来对9个对象进行数学运算,我认为没有任何库存。     

要回复问题请先登录注册