Are you guys searching for the best article based on writing Ns2 source code for wireless sensor network? If yes, then you guys have to take a look about this article. Most significantly, network simulator 2 is defined as the open source software and it is written using some programming languages.

Programming Languages in Ns2

Hereby, we have highlighted the programming languages that have been used in network simulator 2.

  • C++ code
  • It is used to create the proposed algorithm based codes
  • OTcl code
  • It is deployed to configure the network based C++ code objects

Ns2 Code for WSN

For your ease, we have highlighted some sample Ns2 based source code for wireless sensor network.

  • Ns2 code to create a network simulator object and trace files

set ns              [new Simulator]

set tracefd                   [open Sensing.tr w]

set namtrace                [open Sensing.nam w]

  • Ns2 code for packet tracing

struct hdr_cmn *ch = HDR_CMN(p);

            struct hdr_mac802_11* dh = HDR_MAC802_11(p);

            u_int16_t *t = (u_int16_t*) &dh->dh_fc;

            fprintf(stderr, “\t[ %2x %2x %2x %2x ] %x %s %d\n”, *t, dh->dh_duration,

            ETHER_ADDR(dh->dh_ra), ETHER_ADDR(dh->dh_ta), index_, packet_info.name(ch->ptype()), ch->size());

  • NS2 code for virtual address resolution in all sensor nodes

                        if (strcmp(argv[1], “mac-addr”) == 0) {

                                    int n = atoi(argv[2]);

                                    if(!(n < size_))


                                    maddr_[n] = atoi(argv[3]);

                                    return (TCL_OK);


Ping us to know more about the functionalities of network simulator 2!!!