How to generate traffic randomly from a particular node to another node in NS-3

3,024 views
Skip to first unread message

Uddipan Das

unread,
Feb 10, 2015, 3:36:55 PM2/10/15
to ns-3-...@googlegroups.com
Hi,

Could anyone suggest me how I can implement traffic generation randomly from a particular node to another node in a wired network topology in NS-3. I will highly appreciate if anyone can help me with the code.

Thank you very much.




Konstantinos

unread,
Feb 10, 2015, 3:53:41 PM2/10/15
to ns-3-...@googlegroups.com
Hi Uddipan,

When you say random traffic, do you mean variable/random packet inter-arrival time?
Have a look at the different types of applications in NS-3. 
You could use the OnOffApplication which has random On/Off periods, where in On you send with constant bit rate and in Off you do not send anything.
Or you could create your own traffic generator based on the tutorial application class MyApp described in the fifth.cc (http://www.nsnam.org/docs/release/3.22/tutorial/html/tracing.html#the-myapp-application). This uses fixed intervals to schedule the next transmission, you could use random.

Uddipan Das

unread,
Feb 11, 2015, 12:55:10 PM2/11/15
to ns-3-...@googlegroups.com
Hi Konstantinos,

Thank you very much for your reply. Yes, I meant packet generation from a node with random size and random packet inter-arrival time. I tried with OnOffApplication earlier but it does not serve my purpose. However, as you said, I guess fifth.cc example can serve my purpose. I will try to implement it. If I face any problem, it will be highly appreciated if you can help me. I will let you know here later on.

Again thanks a lot.

Uddipan Das

unread,
Feb 15, 2015, 11:46:27 PM2/15/15
to ns-3-...@googlegroups.com
Hello Konstantinos,

I went through the fifth.cc tutorial thoroughly. I have implemented traffic generator in my topology according to the application class MyApp. In that tutorial, only fixed packet size (1040 bytes) is implemented in the Setup function. However, I need to implement random packet size. Could you please suggest me how I can implement random packet size in that application class MyApp.

Thank you very much for your cooperation. Waiting for your reply.

Konstantinos

unread,
Feb 16, 2015, 5:19:33 AM2/16/15
to ns-3-...@googlegroups.com
Hi Uddipan,

If you have understood how to create a random time to generate the packets, you should be able to generate also random packet size.
Instead of creating packets with a constant variable, you can use a random variable to get random values for the packet size.

Uddipan Das

unread,
Feb 18, 2015, 1:09:19 PM2/18/15
to ns-3-...@googlegroups.com
Hi,

In my understanding, random time for each packet generation is created by implementing transmission delay in fifth.cc example.

void
MyApp::ScheduleTx (void)
{
  if (m_running)
    {
      Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
      m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
    }
}


I am confused about how to create random packet size. So could you please tell me how can I use random variable for packet generation. Its better if you can give me an example of code.

Thanks.

Tommaso Pecorella

unread,
Feb 18, 2015, 1:40:27 PM2/18/15
to ns-3-...@googlegroups.com
I beg your pardon, but I don't see any random part in the code you posted. If you believe that it produces random delays, then probably the problem is in that.

I'd strongly suggest to re-read this manual section:

The fact that it's the first chapter in the manual and it's kinda hard to miss makes me think.

Happy reading,

T.

Uddipan Das

unread,
Feb 19, 2015, 1:45:09 AM2/19/15
to ns-3-...@googlegroups.com
Hi Tommaso,

Thanks for your reply. According to your suggestion I revisited manual section of random variables. However, I could not able to generate random packet size through OnOff Application. My code is as follows:


    Ptr<DeterministicRandomVariable> s = CreateObject <DeterministicRandomVariable> ();
    double array [] = {64,128,256,512};
    uint64_t count = 4;
    s-> SetValueArray (array, count);
    double value = s-> GetInteger();

    Ipv4Address SN7_Address (subnet7Interfaces.GetAddress(1));
    uint16_t SN7_port = 8080;
    InetSocketAddress SN7_Socket_Address (SN7_Address, SN7_port);
    OnOffHelper SN7_OnOff("ns3::UdpSocketFactory", SN7_Socket_Address);

 
    Ptr <Packet> p67 = Create <Packet> (value);
    std::cout<<"Packet Size of external Network: "<< p67->GetSize() <<std::endl;

    SN7_OnOff.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=5]"));
    SN7_OnOff.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=10]"));
    SN7_OnOff.SetAttribute("DataRate", StringValue("1Mbps"));
    SN5_OnOff.SetAttribute("PacketSize", UintegerValue(p67->GetSize()));
   
    ApplicationContainer SN7_OnOff_App = SN7_OnOff.Install(host.Get(0));
    SN7_OnOff_App.Start(Seconds(0.0));
    SN7_OnOff_App.Stop(Seconds(50.0));
   
    PacketSinkHelper SN7_Sink("ns3::UdpSocketFactory", SN5_Socket_Address);
    ApplicationContainer SN7_Sink_App = SN7_Sink.Install(routers.Get(1));
    SN7_Sink_App.Start(Seconds(0.0));
    SN7_Sink_App.Stop(Seconds(50.0));

    Simulator::Stop (Seconds (50));
    Simulator::Run ();

Could you please tell me where I did mistake. This code generates only initial value of packet size (512 bytes). I would like to make the packet size random. I will really appreciate your help.

Thanks.

Tommaso Pecorella

unread,
Feb 19, 2015, 2:17:56 AM2/19/15
to ns-3-...@googlegroups.com
Hi,

the only place where you are calling the random number generator is here:
double value = s-> GetInteger();

In order to change a value (and have another random number) you have to call te random number generator "Get" methods. Once set, a variable will not change its value by itself, even if it was initialized by a random number generator.
You are either extremely tired and unfocused, or you have very strange ideas about how random number generators work.

T.

Uddipan Das

unread,
Feb 20, 2015, 3:47:46 PM2/20/15
to ns-3-...@googlegroups.com
Hi,

Problem is resolved now.

Thanks Konstantinos and Tommaso for helping me. Your suggestions were useful to resolve the issue.

Tommaso Pecorella

unread,
Feb 20, 2015, 4:42:05 PM2/20/15
to ns-3-...@googlegroups.com
it's good to know you solved it.

keep up the good work.

T.

Nana

unread,
Jul 18, 2016, 4:52:00 AM7/18/16
to ns-3-users
Hi, U.D.

I'm learning to use ns3 recently and got the same problem. Glad to know you solved it. Would you like to enlighten me or give me any suggestion?

Mohammad Afrashteh

unread,
Sep 15, 2017, 1:25:47 PM9/15/17
to ns-3-users
Best random number generation based on my knowledge is:

#include <chrono>

srand(chrono::duration_cast<chrono::nanoseconds>(chrono::system_clock::now().time_since_epoch()).count()); // for seed

then use

rand()

Thiago Scher

unread,
Nov 30, 2020, 6:03:14 AM11/30/20
to ns-3-users
Hi,

this weekend I had to create a similar simulation and the way I got around this situation was to use the callback function that every time a packet was sent, the packet size attribute was changed. It worked for me.

Regards

Kabeer Ahmed

unread,
Dec 30, 2020, 4:44:22 PM12/30/20
to ns-3-users
Dear thiago

Can you share code or suggestion because I want to change the interval of ping6 application during simulation (for adaptive transmission rate).

Regards

Vignesh K

unread,
Apr 15, 2021, 6:07:17 AM4/15/21
to ns-3-users
Hi U.D,

Greetings!

Can you please let me know, how you have solved this issue. Have you used Myapp concept in fifth.cc or any other way. Pls let me know your inputs.

Thanks,
Vignesh!
Reply all
Reply to author
Forward
0 new messages