question about sending packets periodically and sending the id of the node with it

1,361 views
Skip to first unread message

Ahmed M

unread,
Sep 8, 2015, 8:12:39 PM9/8/15
to ns-3-users
Hello everyone, 

so I start learning Ns3 to use it for researches. I  looked at the examples and read several topics here. I start writing some codes that suppose to send messages between 10 ad hoc nodes. currently it send from two nodes  "it's based on ad-hoc simple example with some modification such as mobility ect". the code work but I want it to send packets every X seconds instead of just sending it once. since I want to treat these messages as beacon messages.

also is there a way to show and print the sender and receiver Id/ip address in the function void ReceivePacket (Ptr<Socket> socket) to make understanding the simulation better? 

any suggestions or pointers will be appreciated. also I would like to thank everyone in this great community for the great support and help you give to all the people trying to learn Ns3. 

sincerely,

Ahmed 
myexample.cc

Konstantinos

unread,
Sep 9, 2015, 4:48:33 AM9/9/15
to ns-3-users
Hi Ahmed,

Sending periodically packets every "X" is already implemented in your code. If you do not understand it, perhaps you need to study your code more carefully. 
Your method GenerateTraffic gives a clue on how to do that:
static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval )

Yes, you can see the ID of the nodes. You need to get that information from the socket and the received packet.
See for example how the PacketSink application handles received packets. The HandleRead method is in principle the same as your ReceivePacket.

Regards,
K.

Ahmed M

unread,
Sep 9, 2015, 9:40:29 AM9/9/15
to ns-3-users


On Wednesday, September 9, 2015 at 4:48:33 AM UTC-4, Konstantinos wrote:
Hi Ahmed,

Sending periodically packets every "X" is already implemented in your code. If you do not understand it, perhaps you need to study your code more carefully. 
Your method GenerateTraffic gives a clue on how to do that:
static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval )

Yes, you can see the ID of the nodes. You need to get that information from the socket and the received packet.
See for example how the PacketSink application handles received packets. The HandleRead method is in principle the same as your ReceivePacket.

Regards,
K.

Hello Konstantinos , 

Thank you for your replay and actually I would like to thank you for all the support you provide to everyone here in this group. I fallowed detailed tutorial for you in old topic it was year and half ago and I wasn't sure if I should thank you there or not since it's old topic so thank you! 

Yes that was my understanding that my code will send a message based on the interval time in my case every 2 seconds...and I thought the code working well until I looked at NetAnim animation. in the animation the nodes move but they send the packets only once. Maybe I misunderstand the animation becasue I only saw arrows come from node 0 and node 1 once only so I thought the packets are sent only once.  thank you so much for the clarification .


I'm going to have a look at  PacketSink application to figure the Id and Ip display for nodes. thank you for the pointers.

sincerely,

Ahmed   

Konstantinos

unread,
Sep 9, 2015, 10:46:13 AM9/9/15
to ns-3-users
Yes, you only send 1packet because you have specified the number of packets to be send to 1

 uint32_t numPackets = 1;

Check again the GenerateTraffic to understand what is the meaning of packetCount (numPackets).

Ahmed M

unread,
Sep 9, 2015, 10:59:33 AM9/9/15
to ns-3-users
Yes this is what I meant when I first asked the question but I guess you made things clear to me now I was looking at the wrong place . correct me if I'm wrong please:

first we define number of packets in: 
uint32_t numPackets = 1;
 
then we define the number of intervals "how long it will take before re sending 1 packet again: 

double interval = 10;

now I'll made tiny change to Generate Traffic function so it look like this: 

static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
                             uint32_t pktCount, Time pktInterval )
{
  if (pktCount > 0)
    {
      socket->Send (Create<Packet> (pktSize));
      Simulator::Schedule (pktInterval, &GenerateTraffic,
                           socket, pktSize,pktCount, pktInterval);

    }
  else
    {
      socket->Close ();
    }
}


I got rid of "-1" 
pktcount-1
 
so now the function won't decrees the number of packets when it's called which will lead to repeat the function until the simulation time finish. which mean the packets will be sent according to the interval number in my case I change the value of intervals to 10 so the packets will be sent from all nodes every 10 seconds. 


thank you again Mr.K !


Konstantinos

unread,
Sep 9, 2015, 11:35:50 AM9/9/15
to ns-3-users
There was a simpler solution without the need of any code change. 
Just make the number of packets very large (e.g. 10^6). Then your application would have to send 10^6 packets. It would took 2*10^6 seconds (if your interval is 2sec) to send all of them. A significant amount of time. 

Ahmed M

unread,
Sep 9, 2015, 8:29:58 PM9/9/15
to ns-3-users
Hello Again Mr.K

so I made some changes on my receive message function to make it look more or less similar to the one in the example you point me to "packetsink.cc" 

I manage to print the sender Ip Address but I couldn't find any example regard printing the receiver ip adress could you please guide me or point me to some examples to figure how to do so  ? also if you could help me figure out how to show the sender and receiver Id I tried node.getid() ; function but it didn't work. 

here is my function: 


void ReceivePacket (Ptr<Socket> socket)
{

Ptr<Packet> packet;
Address from;
while(socket->RecvFrom(from)){


NS_LOG_UNCOND ("Received one packet! \t"  << "At \t " << Simulator::Now ().GetSeconds ()
<< "  from  " << InetSocketAddress::ConvertFrom(from).GetIpv4 ()
<< endl);
    }
}



thank you so much for the great Helo Sir. 

sincerely,

Ahmed 

Konstantinos

unread,
Sep 10, 2015, 8:54:06 AM9/10/15
to ns-3-users
Hi Ahmed,

You can get the receiver node from the Socket, as the socket is aggregated on it. From that you can get the ID of the receiving node.
Now to get the ID of the sending node, or get the IP of the receiving node, you have to create a 'map' function to search which IP is assigned to which interface on each node etc.
This has been discussed in the past in the list from users who were asking how to get the ID of a node knowing their IP. 

Regards
K.

Ahmed M

unread,
Sep 10, 2015, 10:17:43 AM9/10/15
to ns-3-users
Hello Konstantinos , 

thank you for the guide and support you gave to me. for now I guess I'll stuck to finding the ID of the node that received the packet since it sound simpler than finding the Ip of the received node "I honestly want to do this just to confirm that my code work well". 

do you know any example that implement finding the Id of the receiver ? 

also would you please confirm if this function correctly print the Ip of the receiver since I'm still learning Ns3 I feel I'm not very confident with the codes: 


void ReceivePacket (Ptr<Socket> socket)
{


Ptr<Packet> packet;
Address from;
while(socket->RecvFrom(from)){


NS_LOG_UNCOND ("Received one packet! \t"  << "At \t " << Simulator::Now ().GetSeconds ()
<< "  from  " << InetSocketAddress::ConvertFrom(from).GetIpv4 () <<  endl );

    }
}


  thank you so much! 


Konstantinos

unread,
Sep 10, 2015, 10:26:30 AM9/10/15
to ns-3-users
You can test it yourself by creating a simpler example. For example create a scenario with just two nodes, only one of them sending and see if the IP address you get is the correct. 
That's how I would do it and I think that's the easiest way to confirm it.

Regarding on your second question, how to get the ID, I explained how you go about to do it. It is well documented in the manual.
It might not be an example of how to get the Node from a Socket, but the logic behind it is there.

Ahmed M

unread,
Sep 10, 2015, 2:19:23 PM9/10/15
to ns-3-users
Thank You for the support Konstantinos ,

I noticed some weird error in my code not sure what cause it can you explain to me what's wrong there: 

when I write this function twice 

 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                  Seconds (1.0, &GenerateTraffic,
                                  source, packetSize, numPackets, interPacketInterval);


so it look like this: 

  for (int n=0; n<10 ; n++)
  {
 Ptr<Socket> recvSink = Socket::CreateSocket (wifiStaNodes.Get (n), tid);
 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
 recvSink->Bind (local);
 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));

 Ptr<Socket> source = Socket::CreateSocket (wifiStaNodes.Get (n), tid);
 InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
 source->SetAllowBroadcast (true);
 source->Connect (remote);

 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                  Seconds (1.0), &GenerateTraffic,
                                  source, packetSize, numPackets, interPacketInterval);



 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                  Seconds (1.0), &GenerateTraffic,
                                  source, packetSize, numPackets, interPacketInterval);

  }


the code works perfectly and all nodes send and receive however if I wrote only one simulator::ScheduleWithContext

the code doesn't work and none of the nodes send why is that? 

this is the code that doesn't work not sure why I have to write the ScheduleWithContext  twice for it to work 

  for (int n=0; n<10 ; n++)
  {
 Ptr<Socket> recvSink = Socket::CreateSocket (wifiStaNodes.Get (n), tid);
 InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
 recvSink->Bind (local);
 recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));

 Ptr<Socket> source = Socket::CreateSocket (wifiStaNodes.Get (n), tid);
 InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
 source->SetAllowBroadcast (true);
 source->Connect (remote);

 Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
                                  Seconds (1.0), &GenerateTraffic,
                                  source, packetSize, numPackets, interPacketInterval);




  }



thank you so much for all the help and sorry for bothering you with my questions. 
Reply all
Reply to author
Forward
0 new messages