【问题标题】:Implementing packet timeout c++实现数据包超时c ++
【发布时间】:2011-08-09 03:27:50
【问题描述】:

对于一个计算机网络项目,我们正在用 c++ 编写一个具有不同窗口协议的 ftp 服务器。我们在实现工作超时功能时遇到问题。它的要点是在传输数据包时设置时间戳,如果“ack”在一定时间内(比如 2.5 毫秒)没有恢复,则重新传输数据包。我们目前使用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;
    }
}  

其中ptimeout 是发送数据包的时间,connection_parameters-&gt;timeoutInterval 是超时间隔。问题在于,由于ptimeout 是时间的长整数表示,有时它是一个非常大的值(例如 999715992)。这意味着它永远无法判断是否发生了超时,因为在值变得足够大之前,以纳秒为单位的当前时间将恢复为 0。

有其他人用 c++ 处理过这些时间问题并有可能的解决方案吗?

谢谢!

编辑:

感谢您的快速回复!我能够弄清楚一些事情。修改while循环以检查超时+时间戳是否大于允许的长整数大小,让我看看clock_gettime在比较之前是否会变为零。知道这一点后,我检查了当前时间是否 >(超时间隔 - (最大长整数值 - 时间戳))。这允许最多 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;
      }

【问题讨论】:

  • 我当然不是网络方面的专家,但我很确定 TCP/IP 会处理丢失的数据包。为什么不依赖它?
  • @Joce 如果他们只使用预打包的协议,他们将不会学到任何有关网络协议的知识:)
  • @Joce:“对于一个计算机网络项目,我们正在用 c++ 编写一个具有不同窗口协议的 ftp 服务器。” - “窗口”表示重新发送的时间窗口;他们故意重新实现部分 TCP 功能来学习如何做到这一点。
  • @Jeremy @Tony 看,我对网络一无所知! :-)

标签: c++ timeout packets


【解决方案1】:

您可能想将代码更改为:

// time in microseconds
currtime = timeout2.tv_sec * 1000000 + timeout2.tv_nsec / 1000;

只要确保没有任何整数溢出!例如,显式转换为 64 位整数可能是个好主意。

此外,您很可能希望超时至少比 2.5 毫秒大一个数量级,甚至可能更多。例如,您在操作系统中的时间片很可能在 17 毫秒左右,更不用说网络延迟了。

【讨论】:

    【解决方案2】:

    您需要查看clock_gettime 填充的timespec 对象的tv_sectv_nsec 组件。您的ptimeout 字段应该是timespec 而不是整数。您必须编写函数来对 timespec 对象进行数学运算,我认为没有任何库存。

    【讨论】:

      猜你喜欢
      • 2011-06-28
      • 1970-01-01
      • 2023-03-27
      • 2012-04-26
      • 2017-03-25
      • 2015-05-07
      • 2014-05-23
      • 2013-05-06
      相关资源
      最近更新 更多