Getting Started NS3 (Compile and Run Network Simulator Program)

by Agus Kurniawan 1. November 2010 10:22

On the previous article I wrote how to deploy NS3 on Ubuntu 10.10. Now I would like to share how to build and to run your network simulator program into NS3.

Development Tools

As we know, NS3 support development programming C++ and Python.  There are many C++/Python development tools so you choice a tool you have experiences about development.

Getting Started

We start to create folder test on NS3 build folder

n3

Create file hello-world-ns3.cc and write code below ( this code I copy paste from NS3 sample codes, main-simple.cc)

#include <iostream>
 
#include "ns3/core-module.h"
#include "ns3/helper-module.h"
#include "ns3/node-module.h"
#include "ns3/simulator-module.h"
 
using namespace ns3;
 
static void
GenerateTraffic (Ptr<Socket> socket, uint32_t size)
{
  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
  socket->Send (Create<Packet> (size));
  if (size > 0)
    {
      Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
    }
  else
    {
      socket->Close ();
    }
}
 
static void
SocketPrinter (Ptr<Socket> socket)
{
  Ptr<Packet> packet;
  while (packet = socket->Recv ())
    { 
      std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet->GetSize () << std::endl;
    }
}
 
static void
PrintTraffic (Ptr<Socket> socket)
{
  socket->SetRecvCallback (MakeCallback (&SocketPrinter));
}
 
void
RunSimulation (void)
{
  NodeContainer c;
  c.Create (1);
 
  InternetStackHelper internet;
  internet.Install (c);
 
 
  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  Ptr<Socket> sink = Socket::CreateSocket (c.Get (0), tid);
  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
  sink->Bind (local);
 
  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
  InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80);
  source->Connect (remote);
 
  GenerateTraffic (source, 500);
  PrintTraffic (sink);
 
 
  Simulator::Run ();
 
  Simulator::Destroy ();
}
 
int main (int argc, char *argv[])
{
  RunSimulation ();
 
  return 0;
}

Create wscript file and write this script

## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
 
def build(bld):
    env = bld.env_of_name('default')
    if not env['ENABLE_EXAMPLES']:
        return;
    
    obj = bld.create_ns3_program('hello-world-ns3',
                                 ['core', 'simulator'])
    obj.source = 'hello-world-ns3.cc'
 

Create waf file and write this script

exec "`dirname "$0"`"/../waf "$@"

Compile and Run

You can compile and run using ./waf . Make sure you ‘re in test folder and run this script

./waf --run "hello-world-ns3"

Sample of output program you can see picture below

n4

Done. This is a simple program for NS3. If you have question let me know.

Tags:

Linux | Networking | NS

Comments (12) -

Nia
Nia United States
12/26/2010 11:57:54 PM #

Ass mas Agus...

Mau nanya nih....sy dah coba ikutin apa yg mas agus berikan contoh di atas. Tp waktu di run, kok hello-world-ns3 not found ya? Pdhl sy sdh lakukan sama persis spt langkah2 di atas.
Mohon bantuannya...

Tks & Wass, Nia

Agus Kurniawan
Agus Kurniawan Indonesia
12/29/2010 10:10:04 PM #

@Nia
Errornya apa ya? bisa di copy paste? apa instalasi sudah ok? test dengan ./test.py untuk validasi saja;)

Nia
Nia Indonesia
12/30/2010 10:16:44 PM #

Mas Agus...

Sy dah instal ns3-mpls, tp waktu dicompile dgn ./waf, ada yg error, yaitu file ldp-module.h kok ga ada...dimana ya??? Bingunggg....Pernah coba pake ns3-mpls apa blm mas?

Agus Kurniawan
Agus Kurniawan Indonesia
1/5/2011 5:31:51 AM #

wah, instalasi ada yang salah. Coba dilakukan diverifikasi lagi
MPLS, belum dicoba. Saya lagi fokus ke wireless-nya. Logikanya harusnya mudah untuk MPLS;)

C++ Source Code Directory
C++ Source Code Directory United States
1/15/2011 12:45:11 PM #

This was helpful and informative.

learning hypnosis
learning hypnosis United States
1/22/2011 12:31:11 AM #

You have done a great job by sharing such a great informative post with us. I would like to thank you for this.

AADITYA
AADITYA United States
1/24/2011 9:12:33 AM #

GREAT POST.

I NEED SOME HELP TO KNOW IF I MAKE A NEW MODULE IN NS3 HOW DO IT INTEGRATE IN THE NS3.

I ALSO WANT TO KNOW IF ANY GRAPHICAL TOOL CAN BE USED WITH NS3 TO GET DATA OF SIMULATION

THANKS ALOT

teddy juana
teddy juana Indonesia
2/8/2011 9:32:26 PM #

mas agus, saya udah install NS-Nam, tapi saya belum ngerti cara menjalankan program tersebut.. mohon bantuannya ya mas.. terima kasih

ACSG
ACSG Finland
8/23/2011 3:13:49 PM #

Doesn't work for me.... I get the following error:

./waf: line 1: /repos/ns-3-allinone/ns-3.9/build/test/../waf: No such file or directory
./waf: line 1: exec: /repos/ns-3-allinone/ns-3.9/build/test/../waf: cannot execute: No such file or directory

Gabriel Cole
Gabriel Cole United States
9/8/2011 4:27:40 PM #

Astonishingly illuminating bless you, It is my opinion your current subscribers may very well want further blog posts such as this continue the great hard work.

maternity pants
maternity pants United States
1/18/2012 3:36:19 PM #

So glad you shared this find Melanie. Love it! Just more incentive to have another ;)

Heru Sukoco
Heru Sukoco Japan
5/16/2012 6:34:55 PM #

Starting with version 3.11, ns-3 went to a modular directory structure.
Most modules have the same name as before. These module names were changed:

   - core + simulator were merged into core
   - common + node were merged into network
   - internet-stack became internet
   - helper module was removed

The following chapter in the ns-3 Manual shows how modules are organized in the new modular directory structure:

    www.nsnam.org/.../new-modules.html

(source: Mitch Watrous at URL: groups.google.com/.../9ce7652952f28d17)

Heru Sukoco
Heru Sukoco Japan
5/16/2012 6:57:47 PM #

The code should be changed a little bit ^.^:

Old version (< 3.11):
-----
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/helper-module.h"
#include "ns3/node-module.h"
#include "ns3/simulator-module.h"

New version (>= 3.11):
-----
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"

Note:
- core-module.h + simulator-module.h --> core-module.h
- common-module.h + node-module.h --> network-module.h
- internet-stack-module.h --> internet-module.h
- helper-module.h --> REMOVED

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading