ns3介绍与安装

  写博客或者看博客都能使我们的知识更加深刻,也能使我们知道很多新东西。就像这个题目中ns-3,

相信没几个人听过,就是我也是帮同学了解毕设才知道的。工作中相信没几个人用到,但是新知识能

够开阔我们的思维,这也是有些人学习新知识快的的原因。

  ns-3 is a discrete-event(离散事件) network simulator for Internet systems,

targeted primarily for research and educational use.

官网:http://www.nsnam.org

  也就是为研究用的,我感觉唯一的好处或者最大的好处就是学习C++了,

当然使用ns-3网络通信知识必须懂得才行(汗,俺比较薄弱),ns-3基本上是c++,

当然还有python。所以想仿真模拟网络通信可以用C++和python编写,只是对python

支持不完整。ns-3也没ns-2完善,但是对于毕设已经够了。ns-3源码包可以

在官网上下载,目前版本是NS-3.16。

  我在ubuntu安装除了有一点要注意的其他的没有什么问题,我用的python2.7.3,我的系统默认

python就是python2.7.3。解压ns-3源码包后为ns-allinone-3.16,在终端切换到此根目录下。

根据ns-3-tutorial.pdf中的介绍。在终端中输入:

./build.py --enable-examples --enable-tests

#如果build.py没加执行权限可以:

python build.py --enable-examples --enable-tests

ubuntu默认没有g++,需要我们安装,在终端输入sudo apt-get install g++

要注意的是我的build时出错了,错误中有“ascii”字样。主要是系统安装时语言选的是中文,

所以“Desktop”变成了“桌面”,由于python脚本对中文支持的原因,编码出错了。

在脚本中加入#-*- coding:utf-8 -*-可以解决但是不想更改,就直接把ns-allinone-3.16

移动到用户主目录下算了,我就是这么干的。然后再输入上面的build命令就OK了。

build时间不算长但也不短,然后在终端切换到ns-allinone-3.16目录下的ns-3.16目录中。

输入./waf完成后。测试一下:

./test.py -c core

在终端会输出类似这样的信息:

PASS: TestSuite attributes
PASS: TestSuite config
PASS: TestSuite global-value
PASS: TestSuite command-line
PASS: TestSuite basic-random-number
PASS: TestSuite object
PASS: TestSuite random-number-generators
92 of 92 tests passed (92 passed, 0 failed, 0 crashed, 0 valgrind errors)

然后在终端输入:

./waf --run hello-simulator

没意外的话打印的是:

Hello Simulator

Congratulations. You are now an ns-3 user.(引用教程手册的话)

  官方支持的就是类unix系统,但是不是每个人都用,应该说大部分人都不用linux(我是小部分,呵呵)。

也有办法,cygwin应该用过吧,它把linux上的东西移植到windows的,模拟linux环境的软件。

中文站点:http://www.cygwin.cn 但是它仅仅提供了setup.exe。下载后需要在线安装所需的包和工具。

怎样使用google或者百度的啦,这不是本文的重点。运行setup.exe需要安装gcc/g++/python不必说,还有各种

库。尽量多选些。安好后在桌面上有一个叫“Cygwin Terminal”的图表,打开后不用我说相信也知道是什么了吧,

之后的的工作就同ubuntu上build的过程一样啦。

  在ns-3.26/examples/tutorial目录下提供了一些简单的例子。终端当前目录在ns-3.26下,我们可以:

 cp examples/tutorial/first.cc scratch/

然后:

$ ./waf
Waf: Entering directory `/cygdrive/c/Users/cf/Desktop/ns-allinone-3.16/ns-3.16/build'
[ 720/1819] cxx: scratch/first.cc -> build/scratch/first.cc.1.o
[ 721/1819] cxx: scratch/scratch-simulator.cc -> build/scratch/scratch-simulator.cc.2.o
[ 722/1819] cxx: scratch/subdir/scratch-simulator-subdir.cc -> build/scratch/subdir/scratch-simulator-subdir.cc.3.o
[1808/1819] cxxprogram: build/scratch/first.cc.1.o -> build/scratch/first.exe
[1813/1819] cxxprogram: build/scratch/scratch-simulator.cc.2.o -> build/scratch/scratch-simulator.exe
[1814/1819] cxxprogram: build/scratch/subdir/scratch-simulator-subdir.cc.3.o -> build/scratch/subdir/subdir.exe
Waf: Leaving directory `/cygdrive/c/Users/cf/Desktop/ns-allinone-3.16/ns-3.16/build'
'build' finished successfully (28.098s)

最后运行:

$ ./waf --run scratch/first
Waf: Entering directory `/cygdrive/c/Users/cf/Desktop/ns-allinone-3.16/ns-3.16/build'
Waf: Leaving directory `/cygdrive/c/Users/cf/Desktop/ns-allinone-3.16/ns-3.16/build'
'build' finished successfully (6.926s)
At time 2s client sent 1024 bytes to 10.1.1.2 port 9
At time 2.00369s server received 1024 bytes from 10.1.1.1 port 49153
At time 2.00369s server sent 1024 bytes to 10.1.1.1 port 49153
At time 2.00737s client received 1024 bytes from 10.1.1.2 port 9

  后续有什么值得记录的,还会继续......

原文地址:https://www.cnblogs.com/wuchaofan/p/3026282.html