首页 > 解决方案 > 如何在 NS-3 中使用 TCP 获取丢失数据包的数量?(80211n + MIMO 示例)

问题描述

我正在使用这个例子:https ://github.com/nsnam/ns-3-dev-git/blob/master/examples/wireless/80211n-mimo.cc 。对于 UDP,在 UdpServer 中有一个名为“GetLost()”的函数,它返回丢失的数据包的数量,但对于 TCP,没有。请问有没有办法解决这个问题。谢谢

      /* Setting applications */
      ApplicationContainer serverApp;
      if (udp)
        {
          //UDP flow
          uint16_t port = 9;
          UdpServerHelper server (port);
          serverApp = server.Install (wifiStaNode.Get (0));
          serverApp.Start (Seconds (0.0));
          serverApp.Stop (Seconds (simulationTime + 1));

          UdpClientHelper client (staNodeInterface.GetAddress (0), port);
          client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
          client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
          client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
          ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
          clientApp.Start (Seconds (1.0));
          clientApp.Stop (Seconds (simulationTime + 1));
        }
      else
        {
          //TCP flow
          uint16_t port = 50000;
          Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
          PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
          serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
          serverApp.Start (Seconds (0.0));
          serverApp.Stop (Seconds (simulationTime + 1));

          OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
          onoff.SetAttribute ("OnTime",  StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
          onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
          onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
          onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
          AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
          onoff.SetAttribute ("Remote", remoteAddress);
          ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
          clientApp.Start (Seconds (1.0));
          clientApp.Stop (Seconds (simulationTime + 1));
        }        

    Simulator::Stop (Seconds (simulationTime + 1));
      Simulator::Run ();

      uint64_t nb = 0;
      uint64_t lost = 0;
      if (udp)
        {
          //UDP
          nb = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
          lost = DynamicCast<UdpServer> (serverApp.Get (0))->GetLost();
          double window = DynamicCast<UdpServer> (serverApp.Get (0))->GetPacketWindowSize();
          std::cout << "Window Size :" << window << std::endl;
        }
      else
        {
          //TCP
          uint64_t totalPacketsThrough = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
          nb = totalPacketsThrough / payloadSize ;

          /*need help here to get the number of lost packets using TCP Thanks*/

        }
      dataset1.Add (d, nb);
      dataset2.Add (d, lost);
      std::cout << nb << " Received packets" << std::endl;
      std::cout << lost << " Lost packets" << std::endl;
      d += step;
      Simulator::Destroy ();
    }
  plot1.AddDataset (dataset1);
  plot2.AddDataset (dataset2);
}

标签: ns-3

解决方案


每次传输数据包时,使用 OnOffApplication 的“Tx”跟踪调用函数 -

void SourceUdpTrace(Ptr<const Packet> pkt)
{
  nPacketsSent++;
}

在 main() 中,使用以下 Config::Connect 语句设置跟踪并将其与SourceUdpTrace. 该语句必须放在前面Simulator::Run()

Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::ManualOnOffApp/Tx",MakeCallback(&SourceUdpTrace));

一旦您知道发送数据包的数量,您可以通过比较它的值轻松确定丢失数据包的数量nb,如您的代码行所发现的 -nb = totalPacketsThrough / payloadSize;


推荐阅读