How to get the number of hops through the packet

110 views
Skip to first unread message

Sun Yu

unread,
May 7, 2017, 3:01:27 AM5/7/17
to ns-3-users
Hello everyone。 I am a fresher about NS3. Now, I want to get the number of hops through the packet when distination node recieve packet. Next is my code that is modified from manet-routing-compare.cc(examples/routing/...)
  I find a function (uint_8 GetHopCount()const) but i don`t know how to use it .
uint32_t currentSequenceNumber;
void ReceivePacket1 (Ptr<Socket> socket)
{
  
  NS_LOG_UNCOND ("Received One packet!");

   Ptr<Packet> packet ; 
//
//  PacketHeader packeheader = GetHopCount();
   while(packet = socket->Recv())
    {
        SeqTsHeader seqTsx;
        packet->RemoveHeader (seqTsx);
         currentSequenceNumber = seqTsx.GetSeq ();

        
            bytesTotal += packet->GetSize ();
                  packetsReceived += 1;

        
            rcv = Simulator::Now().GetMilliSeconds();
            sqhd = seqTsx.GetTs().GetMilliSeconds(); 
            
            std::cout << "Seq No " << currentSequenceNumber << " Tx " << sqhd << "\n";//Just to check seq number and Tx time 
             delay = delay +  (rcv - sqhd); //delay calculation 
           std::cout << "delay" << ":" << rcv - sqhd <<"  " << rcv << std::endl;
        // std::cout << "DFDF" << ":" << Simulator::Now().GetMilliSeconds<< std::endl;
    }
             xx = rcv - senttime;    
               
        throughput = (bytesTotal * 8 * 1000) / (1024 * xx); //throughput calculation
 
}


  static void GenerateTraffic (Ptr<Socket> socket/* Ptr<Packet> p*/, 
                            uint32_t pktSize, 
                             uint32_t pktCount, 
                              Time pktInterval )
{
  if (pktCount > 0)
    {
        SeqTsHeader seqTs;
      seqTs.SetSeq (pktCount);
      Ptr<Packet> p = Create<Packet> (packetSize-(8+4)); // 8+4 : the size of the seqTs header
      p->AddHeader (seqTs);
      socket->Send (p);
      NS_LOG_UNCOND ("Sending "<< pktCount  << " packet! \n");
      if(pktCount == 25)
    {
        senttime = Simulator::Now().GetMilliSeconds();

        }     

    Simulator::Schedule (pktInterval, &GenerateTraffic, 
                           socket, pktSize,pktCount-1, pktInterval);
    }
        
    
  else
    {
      socket->Close ();
    }
}
I  use these code from other post is run ok.
manet-routing-compare.cc

Tommaso Pecorella

unread,
May 7, 2017, 5:55:03 AM5/7/17
to ns-3-users
Hi,

GetHopCount is a function of PbbPacket - a rather "strange" kind of packet,  not really used in ns-3.

What you're looking for is the packet TTL, and you can read it through a tag.
Check the example examples/socket/socket-options-ipv4.cc 

T.
Reply all
Reply to author
Forward
0 new messages