安装NS2进行网络仿真

1. 目的

搭建NS2环境,实现隐藏节点的模拟。

2. 环境安装

环境安装中多需要sudo来提权,若出现Permission Denied等字样,可先检查是否已经赋予正确权限。

2.1 Ubuntu

从网络中获取Ubuntu的镜像,这里选择的是Ubuntu 14桌面版32位。镜像可以在Ubuntu的官方站点获取,http://www.ubuntu.org.cn/desktop。

在虚拟机安装成功后如图所示,进入主页面。

Ubuntu主界面

2.2 安装依赖

使用以下命令安装依赖

apt-get update
apt-get install build-essential tcl8.4 tcl8.4-dev tk8.4 tk8.4-dev libxmu-dev libxmu-headers

2.3 NS-Allinone安装

从网络中下载到ns-allinone-2.35.tar.gz安装包,在本地解压缩按安装包:

tar xvzf ns-allinone-2.35.tar.gz

进入ns-allinoe-2.35目录,此时,需要修正一些信息以保证接下来的编译正常以及正常使用mUDP.

以下做法是用来应对./install时产生的以下错误:

产生错误提示:linkstate/ls.h:137:58: note: use ‘this->erase’ instead

具体错误提示:

linkstate/ls.h:137:58: note: declarations in dependent base ‘std::map<int, LsIdSeq, std::less<int>, std::allocator<std::pair<const int, LsIdSeq> > >’ are not found by unqualified lookup
linkstate/ls.h:137:58: note: use ‘this->erase’ instead

解决办法:

编辑ns-allinone-2.35/ns-2.35/linkstate/ls.h中的135行,

void eraseAll() { erase(baseMap::begin(), baseMap::end()); }

改为

void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); }

如图所示

修改eraseAll


进入ns-allinone-2.35目录,输入./install进行安装.再出现了一大堆信息后,安装应当已经成功。

出现的信息应当与此类同:

Please put /home/WNT/Downloads/ns-allinone-2.35/bin:/home/WNT/Downloads/ns-allinone-2.35/tcl8.5.10/unix:/home/WNT/Downloads/ns-allinone-2.35/tk8.5.10/unix
into your PATH environment; so that you’ll be able to run itm/tclsh/wish/xgraph.

IMPORTANT NOTICES:

(1)You must put /home/WNT/Downloads/ns-allinone-2.35/otcl-1.14,/home/WNT/Downloads/ns-allinone-2.35/lib,
into your LD_LIBRARY_PATH environment variable.
If it complains about X libraries, add path to your X libraries
into LD_LIBRARY_PATH.
If you are using csh, you can set it like:
setenv LD_LIBRARY_PATH
If you are using sh, you can set it like:
export LD_LIBRARY_PATH=

(2) You MUST put /home/WNT/Downloads/ns-allinone-2.35/tcl8.5.10/library intoyour TCL_LIBRARY environmental
variable. Otherwise ns/nam will complain during startup.

After these steps, you can now run the ns validation suite with
cd ns-2.35; ./validate

For trouble shooting, please first read ns problems page
http://www.isi.edu/nsnam/ns/ns-problems.htmlAlso search the ns mailing list archive
for related posts.

此时需要编辑.bashrc, 按照上述提示,在~/.bashrc中追加一下内容

export PATH="$PATH:/home/WNT/Downloads/ns-allinone-2.35/bin:/home/WNT/Downloads/ns-allinone-2.35/tcl8.5.10/unix:/home/WNT/Downloads/ns-allinone-2.35/tk8.5.10/unix"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/WNT/Downloads/ns-allinone-2.35/otcl-1.14:/home/WNT/Downloads/ns-allinone-2.35/lib"
export TCL_LIBRARY="$TCL_LIBRARY:/home/WNT/Downloads/ns-allinone-2.35/tcl8.5.10/library"

保存后退出。在Terminal中输入ns,出现以下内容,则表示成功:

2.4 实现mUDP环境

  1. 下载mudp.cc、mudp.h、mudpsink.cc、mudpsink.h,放置在ns-allinone-2.35/ns-2.35/measure文件夹中,若measure不存在则需要自行建立。
  2. 修改mudp.cc,将以下代码:
mUdpAgent::mUdpAgent() : id_(0), openfile(0)
{
    bind("packetSize_", &amp;size_);
    UdpAgent::UdpAgent();
}

改动为:

mUdpAgent::mUdpAgent() :UdpAgent(),id_(0), openfile(0)
{
    bind("packetSize_", &amp;size_);
}
  1. 修改ns-allinone-2.35/ns-2.35/common/packet.h,加入以下内容至struct hdr_cmn{}中:
int frametype_;
double sendtime_;
unsigned int pkt_id_;
unsigned int frame_pkt_id_;

如图所示

  1. 修改ns-allinone-2.35/ns-2.35/Makefile,追加以下内容至OBJ_CC:

measure/mudp.o measure/mudpsink.o

  1. 修改ns-allinone-2.35/ns-2.35/tcl/lib/ns-default.tcl, 追加一下一下内容在Agent/Ping set packetSize_ 64下面:

Agent/mUDP set packetSize_ 1000

  1. ns-allinone-2.35/ns-2.35目录下make clean; make即可。

3. 实验检测

进入到相应Examples的目录,输入ns XXXX.tcl即可开始仿真。

最后运行隐藏节点的仿真效果如图所示。