网络仿真与模拟实验报告_第1页
网络仿真与模拟实验报告_第2页
网络仿真与模拟实验报告_第3页
网络仿真与模拟实验报告_第4页
网络仿真与模拟实验报告_第5页
已阅读5页,还剩61页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、网络仿真和模拟课程设计实验报告学院:计算机学院专业:网络工程组员(学号): 王荣达(13055522)、李晨阳(13055318)、张勇安(13055534)指导老师:郑秋华完成时间2016年6月30日目录 TOC o 1-5 h z HYPERLINK l bookmark1 o Current Document 网络仿真和模拟课程设计实验报告1 HYPERLINK l bookmark4 o Current Document 目录2 HYPERLINK l bookmark13 o Current Document 一、NS2模拟环境搭建1 HYPERLINK l bookmark19 o

2、 Current Document 安装 cygwin1 HYPERLINK l bookmark31 o Current Document 安装 NS27 HYPERLINK l bookmark43 o Current Document 二、不同版本的TCP协议仿真模拟112.1实验目的11 HYPERLINK l bookmark47 o Current Document 2.1实验步骤11 HYPERLINK l bookmark55 o Current Document 2.2实验过程及结果比较11三、添加Ping协议18 HYPERLINK l bookmark104 o Curr

3、ent Document 3.1实验目的18 HYPERLINK l bookmark82 o Current Document 3.2实验步骤18 HYPERLINK l bookmark101 o Current Document 四、添加 Application 和 Agent 协议234.1实验目的23 HYPERLINK l bookmark107 o Current Document 4.2实验步骤23 HYPERLINK l bookmark139 o Current Document 附录A29 HYPERLINK l bookmark142 o Current Documen

4、t 附录B32 HYPERLINK l bookmark145 o Current Document 附录C36 HYPERLINK l bookmark148 o Current Document 附录D46 HYPERLINK l bookmark154 o Current Document 附录E47 HYPERLINK l bookmark160 o Current Document 附录F50一、NS2模拟环境搭建在Windows操作系统下安装比较繁琐,需要根据NS不同版本,下载相关补 丁进行修正。下面介绍在 Windows下安装NS-allinone过程。1.1 安装 cygwin

5、1)点击安装setup.exe文件,如图1所示:图1执行setup.exe文件2)请点击“下一步”按钮,如图2所示:匚 Cygwin Setup - Choose Installation TypeChoose A Download SourceChoose whetherto install or download from the internet, or install from files in a local directory.Install from Internet:熟口质虫4倾照理W.b.u.K既土旭Uybum.旧Download Without InstallingInst

6、all from Lpcal Directory上一步但)下一步/)M 取消图2安装步骤23)选择“Install from Internet”单选按钮,如图3所示:匚 Cygwin Setup - Choose Installation TypeChoose A Download SourceChoose whetherto install or download from the internet, or install from files in a local directory.6-Install from Interneti袖口旭脸史4倾黑理!.场.K既土旭廿姬照*Download

7、 Without InstallingInstall from Lacal Directory上一步遂)|下一步/) 取消图3安装步骤34)点击“下一步”按钮,如图4所示:图4安装步骤45)点击“下一步”按钮,如图5所示:匚 Cygwin Setup - Select LdcsI Package DirectoryBrowse.Select Local Package DirectoiySelect a directory where you want Setup to stone the installation files it downloads. The directory will

8、be created if it does not already exist.Local Package DirectoryD:Downloads上一步遂)|下一步/) 取消图5安装步骤56)点击“下一步”按钮,如图6所示:图6安装步骤67)点击“下一步”按钮,如图7所示:图7安装步骤78)点击“下一步”按钮,如图8所示:图8安装步骤89)选择“Curr”,点击“View”按钮,使右方出现“full”,然后把最下角 的隐藏过期包组件那个勾去掉,如图9所示:匚 Cygwin Setup - Select PackagesSelect PackagesESelect packages to in

9、stallSearch Keep 通 Qjrr 。与 View FullCurrentNewBin.? Src? Categori esSi zePackage砂 SkipriiahiqG:m 弥10k 2048_cli: 2SkipriiahiqDebug28k 2048- cl i - de砂 SkipnijniijG:diri e e1_, 37Uk 2048- qt: 204 Skipn.iuhiqDebug2,253k 2048-qt_ deb4Skipn.iuhiqDebug3,964k 4ti2-iiebugi4 SkipnijniQAudi 口109k Atomi cF:ii-

10、sl4 SkipnijniQDebug266k Atomi cF:ii-slSkipn/a n/Q Libs., Math105k CUmt: CUniSkipriiahiqDebug72k CUni t_ debug4 Skipn.iuhiqLevel, Libs, Sys tern, 1J694k ELFI0: ELF4 SkipnjQn/Q GNOME310k XorL2: GN04Skipn/QniQDebug661k lXorLt2-debu4 Skipn/QniQNet27k GeoIF: Geo I4Skipn/QniQNet24., 288k GeoIF-databSkipri

11、iahiqDebug104k GecIF-debugSkipn.iunfaGraph,827k Graphics Mag T* 1卜n地导职gl琪目pggK.阳感|上一步II下一步 1 取消 1图9安装步骤910)由于是初次接触该软件,在不清楚各个包作用的情况下将所有的包都安装,然后点击“下一步”按钮,如图10所示:图10安装步骤1011)安装结束后,点击“完成”按钮,如图11所示:图11安装步骤111.2安装NS21)解压缩并改变路径到ns-allinone-2.35,如图12所示:127128129130131 132133134135136 | |138日139140141图12切换到N

12、S目录2)修改 ls.h 错误代码行(进入 C:cygwinhome/Administartor/ns-allinone -2.35/ns-2.35/linkstate/ls.h 第 137 行):/ this next typedef of iterator seems estraneous but is required by gcc-2.96 typedef typename mapKeyj T, less : : iterator iterator: typedef pair pair_iterator_bool: iterator insert (const Key & key co

13、nst T & item) typename baseMap: :value_type v(key item);pair_iterator_bool ib = baseMap:insert(v): return ib. second ? ib. first : baseMap: : end ():void eraseAll () this-erase (baseMap:beginOj baseMap: end() : T* findPtr(Key key) iterator it = baseMap: find (key):return (it = baseMap: end() ? (T *)

14、NULL : & (*it). second):142;3)安装指令,进行编译安装,如图13所示:图13进行编译安装4)重新编译完后,如图14所示:图14编译结果5)设置环境变量:6)测试NS2是否安装成功,如图15所示:Main Options VT Options VT FontsAdministrator-PC-20160610QVCE /ns-allinone-2t35 $ nsX1+已停止nsAdministrator-PC-20160610QVCE w/ns-allinone-2t35图15进入图形化接口窗口7)在跳出的对话框中,输Anam,系统跳出一个图形界面,如图16所示:图1

15、6 Nam界面1回8)在窗口中输入gnuplot,结果如图17所示,则NS2安装成功:釜 /ns-allinone-235Main Options VT Options VT FontsAdministrator-PC-20160610QVCE /ns-allinone-2t35 $ nsX1+已停止nsI Administrator-PC-20160610QVCE w/ns-allinone-2t35$ namAdministratorPC-20160610QVCE w/ns-allinone-2t35$ gnuplotGNUPLOTVersion 5+0 patchlevel 3 last

16、 modified 2016-02-21Copyright (C) 1906-1993, 1998, 2004, 2007-2016Thomas Williams Colin Kelley and many othersgnuplot home: http:/www+gnuplot + infofaq, bugs etc: type help FAQimmediate help: type help (plot window: hit h)Terminal type set to xll gnuplot |、!二、不同版本的TCP协议仿真模拟2.1实验目的通过本实验了解如何利用OTcl脚本编写

17、NS仿真案例。2.1实验步骤NS2下不同版本的TCP协议的OTcl脚本编写。在winx-server模式下键入命令:ns 文件名.tcl运行ns文件。对实验结果进行分析比较。2.2实验过程及结果比较 Tahoe执行方法,如下所示:ns lab11.tcl Tahoe (lab11.tcl 文件代码参考附录 A,TCPV1)Administr-atorykz-20100502XQS /ns-al1inone-2+33/ns-2+33/tcl/ex/shi1i/labll $ nelabll+tcl Tahoeaverage throu9hput:346+4 Kbpsgnuplot 进入 gnup

18、lot提示符gnuplat set title Tahoe9muplot set xlabelgnuplot set 与】初巳】cwnd1 grfljplot set terwinaL giF Terntinal typs set to gif Options are nckcrop tnediun gmuplot set output cwnd-Tahoeif11 gnuplot plot cwnd-Tahoe,tr with linespoints会在目录下生成gif图片,见下面图片比较。 Reno执行方法,如下所示:Administratorykz-20100502XQS /ns-a11

19、inone-2133/ns-2133Ztc1/ex/shili/labll $ ns labll+tcl Renoaverage throughput:940 set relabel tie 9nuplot set gUbel 七wrcT enuplot set ttrmin*! gif TenoirMl tgp曰 set to gif Options are nocrop medium 1 gnupluO set output cwnd-Renti+gifM gnuplot lot cund-Rerwtr with lintspointsNewReno执行方法,如下所示:(lab12.tcl

20、)Adm ini str-atorykz-20100502XQSHi nnne-2+33/ris-2+33/tc 1 /ex/sh i li/labl2$ ns labl2;+tcl Newreno on path: 16+44 packets average throughput: 956+8 Kbp$-gngkO sfft titlt *ffe*4?eno 9rM*lW 辑t xlabai tiM 曹呻S3圳1 cwrF 就setgifTernml type to gifOptionsnocrop 庭皿 ,9rx451ttutpot gnd-Nt曲trm.g讦-plot CMrtd-Nsw

21、refto+tr- with UnefpojritsSACK执行方法,如下所示:Administr-atorykz-20100502XQS Vns-al 1 inone-2+33/ns-2+33/tcl/ex/shi 1 i/labl2 $ ns labl2+tcl Sack on path: 16+44 packets average throughput: 958+4 Kbps-9rtuploi set title Sack gplotwt jclabtl tiy ynuplot set ylatel %血” gplotset terwinl gif Tcrninal type set t

22、o gif*Options are nxnjp med 1 U *gnuplot set output 匚蛔-轮株禅件* qnuplotpl夙cwnd-Ssck uith linesointsVegas执行方法,如下所示:(lab13 1.tcl)Administratorykz-20100502XQS w/ns-al1inone-2+33/ns-2.33/tcl/ex/shili/labl3 $ ns labl3_l+tclsocketerVM_5_104_centos experiment? ns labl3_l*tel average throughput: 298*4 Kbps ave

23、rage throughput: 678*4 Kbpsgnuplot set title “Vegas”gnuplot set xlabel time”gnuplot set ylabel cwnd”gnuplot set terminal gifgnuplot set output “cwnd-Vegas”gnuplotplot cwnd0.tr” with linespoints lt 1, “cwnd1.tr” with linespoints lt 2 Vegas和Re执行方法,如下所示:(lab13 2.tcl)1 . ns lab13_2.tcl2.gnuplot set titl

24、e Vegas vs Reno”gnuplot set xlabel time”gnuplot set ylabel cwnd”gnuplot set terminal gifgnuplot set output “cwnd-Vegas-Reno”gnuplotplot cwnd-vegas.tr” with linespoints lt 1, “cwnd-reno.tr” with linespoints lt结果比较Tahoe012345678Q10time图16 Tahoe的cwnd变化图图17 Reno的cwnd变化图图 18 TCP NewRenoSack25201510501210

25、86420012345678910time图 19 SACKVegas;cwnd0.tr4-cwndl.tr 一*012345678910time2520151050vfegas-Reno10time图21 Vegas与Reno的cwnd变化图结果分析从Tahoe的cwnd变化图(图16)中我们可以看出,TCP的Congestion Window 值会呈现周期性的重复变化。开始时,先由Slow-start开始,cwnd超过Ssthresh 时进入Congestion Avoidance阶段。由于传送到网络上的封包不断地增加,当超 出允许能传送到网络上的个数时,路由器开始使用Drop-tail将

26、封包丢掉。当有 封包遗失时,Tahoe这个TCP版本会将ssthresh设为发现到封包遗失时的Window 值的1/2,接着将Window的值设为1。在Tahoe这个例子中,由于只要有封包 遗失,Cwnd的值就会被重置为1 (小于Ssthresh),因此每次封包遗失,Tahoe 都重新由slow-start开始。从Reno的cwnd变化图(图17)中看到,当检测到封包遗失时,ssthresh 和cwnd的值会被设为先前cwnd值的1/2。因此在重送遗失的封包后,TCP Reno 会由 Congestion Avoidance 开始。由于结束 Fast recovery 后,Reno 的 cwn

27、d 由先 前cwnd值的1/2开始增加,所以得到的平均吞吐量较Tahoe为佳。从该图中我 们还可以看到,当TCP的传送端观察到端点到端点的路径并没有拥塞的情况时 (没有检测到封包遗失),会持续地以累加的方式增加传送速率。但是当检测到 路径拥塞的情况时,则以倍数的方式减少传送速率。基于上述原因,TCP的拥塞 控制算法又常被称为累加递增-倍数递减的算法。从TCP NewReno的cwnd变化图(图18)中我们可以看到,NewReno在收 到Partial ACK时,并不会立刻结束Fast-recovery,相反,NewReno的传送端会 持续地重送Partial ACK之后的封包,直到将所有遗失的

28、封包重送后才会结束 Fast-recovery,这使得NewReno的传送端在网络有大量封包遗失时不需等待 Timeout就能更正此错误,减少大量封包遗失对传输效果所造成的影响NewReno 大约每一个RTT时间可重送一个遗失的封包,在Fast-recovery阶段,若允许的 话,传送端可以继续送出新的封包,以增加Link的使用率。从TCP SACK的cwnd变化图(图19)中我们可以看到,在这个版本中, 加入了一个SACK选项,允许接收端在返回Duplicate ACK时,将已接收到的数 据区段(连续收到的数据范围)返回给传送端,数据区段与数据区段之间的间隔 就是接收端没有收到的数据。通过这

29、些信息,传送端就知道哪些是已经收到的, 哪些是该重送的,因此SACK的传送端可以在一个RTT时间之内重送一个以上 的封包。从Vegas的图形(图20)中可以看到,第一条Vegas从0s开始执行,10s 时结束。第二条Vegas在5s时才开始执行,10s时结束。从图中我们还可以看到, 在Slow-start阶段,cwnd的值大约两个RTT才会增加1倍。与Reno不同的是, 当Diff的值介于a与6之间时,Vegas的cwnd会持续维持在一个稳定的状态, 这是因为,基本上Vegas的拥塞控制算法是一种“拥塞避免”的方法。从Vegas与Reno的cwnd的变化图(图21)中可以看到,Reno的 Wi

30、ndow 总是在较高的地方振荡,而Vegas的Window总是在较低的位置。虽然根据研究 显示,当网络的通信都使用TCP Vegas时,整体的执行效果会优于TCP Reno, 这是因为TCP Vegas采取较为保守的作法避免封包遗失,并以此提高网络的执行 效果。但当TCP Vegas和Reno共存时,TCP Vegas并没有办法与TCP Reno公平 地竞争频宽。造成这个问题的主要原因是,Reno使用了较具侵略性的拥塞控制 方法,TCP Reno的传送端会持续地将封包送到网络上直到发生拥塞。比较之下, Vegas的传送端在网络开始拥塞时就将传送端的传送速度降慢,以避免拥塞的情 形发生。因此,当

31、TCP Vegas与TCP Reno共存Vegas在效果上的表现总是会比 较差。三、添加Ping协议3.1实验目的本实验通过实现一个Ping协议来说明如何在NS2中添加新协议3.2实验步骤一、在ns-2.35目录下新建一个MyPing目录,将老师给的文件MyPing.h和MyPing.cc 放到这个目录里下。这两个文件见 附录D和E。(红色字体为修订处)文件说明:MyPing.h 中:定义了包头结构:struct hdr_ping (char ret;double send_time;定义PingAgent类class PingAgent : public Agent (public:Ping

32、Agent();int command(int argc, const char*const* argv);void recv(Packet*, Handler*);protected:int off_ping_;MyPing.cc 中:定义 PingHeaderClass 类static class PingHeaderClass : public PacketHeaderClass (public:PingHeaderClass() : PacketHeaderClass(PacketHeader/Ping,sizeof(hdr_ping) ( class_pinghdr;定义PingCl

33、ass类static class PingClass : public TclClass (public:PingClass() : TclClass(Agent/Ping) TclObject* create(int, const char*const*) ( return (new PingAgent(); class_ping;定义PingAgent的实现PingAgent:PingAgent() : Agent(PT_PING)( bind(packetSize_, &size_);bind(off_ping_, &off_ping_);int PingAgent:command(in

34、t argc, const char*const* argv)(if (argc = 2) (if (strcmp(argv1, send) = 0) (return (Agent:command(argc, argv);void PingAgent:recv(Packet* pkt, Handler*) 修改 ns-2.35/common/packet.h 文件如图找到相应的地方添加修改:(代码前面的+表示添加,-表示删除,下同) -200,7 4-200,8 施 static const packet_t PT_DCCP_RESET = 71; static const packet_t

35、PT_MDART = 72; insert new packet types here-static packet* PT_NTYPE = 73; / This MUST be the LAST one +static const packet_t PT_MYPING = 73;+static packet_t 一 Paccess (offset );/ Multimedia Header Classstatic class Mult line di aHeader Class : public PacketHeader Class public:Mult line di aHeader Cl

36、ass () : PacketHeaderC lasssizeof (hdr_iim) ) ( bind_offset (hdr_inn: : of f set_);) 一 一 class imihdr;编写 MmApp Senderclass SendTlmer : public Tinier Handler public :SendTimer (MmApp* t) : TitnerHandler () , t._ (t) inline virtual void expire(Event*);protected:MttiApp* t_;; 一void SendTlmer:expire(Eve

37、ntt_- 3 e nd_mri_p kt ();class MnApp : public Application ( public:HmApp(); private:SendTiiner snd tinier ;t&nApp : : MmApp () : running_ (0) , snd_tinier_ (this) f ack_tiiner_ (this) 一 一 bind_tow (rrrate0_rr, firate );bind_bw (rprate4_r firate 4);bind (,rpktsize_r pktsize_);bind bool (random rr, fi

38、random );void MiApp : : SEnd_niri_pkt () hdr_imi ttih_buf;if (running_) (agent_-sendinsg (pktsize_, (char*) inh_buf) ;/ send to UDP/ Reschedule the send_pkt timer doukile next_time_ = next_snd_time ();if (next time 0) snd tinier . reached (next time );编写 MmApp Receiver具体见附录。编写 UdpMmAgentUdpMmAgent继承

39、了 UdpAgent,添加了部分功能。具体见附录。修改 ns-2.35/packet.h在如图两处,各添加一行,并做相应修改。施-201,7 +201,8 函 static const packet* PT_MDART = 72;/ insert new packet types herestatic const packet_t PT_MYPING = 73;-static packet_t 一 PTNTYPE = 74; / This MUST be the LAST one +static const packet_t PT_MULTIMEDIA = 74;+static packet_

40、t - PTNTYPE = 75; / This MUST be the LAST oneenum packetclass施-418,6 +419,7 施 public:name_PT_DCCP_CLOSEREQ=recv(pj h); virtual void timeout(int tno);五、修改 ns-2.35/apps/app.h:阚-5饥6 +5豹7 鼬 public:Application。;virtual voidvirtual voidvirtual voidvirtual voidsend(int nbytes);recv(int nbytes);recv_msg(int

41、 nbyteSj const char *msg = O) );resume();protected:六、在ns-2.35/tcl/lib/ns-default.tcl中设置新参数的默认值 -505?6 4-50517 Application/SctpAppl set numStreams_ 1Application/SctpAppl set numUnreliable_ 0Application/SctpAppl set reliability_ 0+4tMultimedia4-Application/MmApp4-Application/MmApp4-Application/MmApp4-

42、Application/MmApp4-Application/MmApp4-4-Application/MmApp+Applicatian/MmApp4-set rateO_ 0*3mb set ratel_ 0*6mb set rate2_ 0*9mb set rate3_ l*2mb set rate4_ 1* 5mbset pktsize_ 1000 set random_ falseRandomVariable/Uniform set min_ 0*GRandomVariable/Uniform set max_RandomVariable/Exponential set avg_ 1

43、.0七、编辑 ns-2.35/Makefile : -334,6 4-334,7 施 OBJ CC = wpan/p802_15_4sscs*o wpan/p802_l5_4timer,o wpan/p802_15_4trace*o wpan/p802_15_4transac*o apps/pbc*o + Multimedia/mm-app*o Multimedia/udp-mm*o $(OBJ_STL)八、在Makeflie目录下编译:make cleanmake dependmake九、运行MnApp_SMmApp_RUDPimiUDPmmtcp (ttiax_win =15Jsink编写

44、测试的TCL代码。见附录F :ex-mm-app.tclftp十、运行程序ns ex-mm-app.tcl附录ATCPV1代码if $argc !=1 puts Usage: ns labll.tcl TCPversion puts Example:ns lab11.tcl Tahoe or ns lab11.tcl Renoexitset par1 lindex $argv 0#产生一个仿真的对象set ns new Simulator$ns color 1 Blue#开启一个trace file,用来记录封包传送的过程set nd open out-$par1.tr w$ns trace-

45、all $ndset nf open out-$par1.nam w$ns namtrace-all $nf#开启一个档案用来记录cwnd变化情况set f0 open cwnd-$par1.tr w#定义一个结束的程序proc finish global ns nd nf f0 tcp par1#显示最后的平均吞吐量puts format average throughput:%.1f Kbps expr $tcp set ack_*($tcp set packetSize_)*8/1000.0/10$ns flush-trace#关闭档案close $ndclose $nfclose $f

46、0exec nam out-$par1.nam &exit 0#定义一个记录的程序#每格0.01秒就去记录当时的cwndproc record (global ns tcp f0set now $ns nowputs $f0 $now $tcp set cwnd_$ns at expr $now+0.01 record#产生传送节点,路由器r1,r2和接收节点set n0 $ns nodeset r0 $ns nodeset r1 $ns nodeset n1 $ns node#建立链路$ns duplex-link $n0 $r0 10Mb 1ms DropTail$ns duplex-li

47、nk $r0 $r1 1Mb 4ms DropTail$ns duplex-link $r1 $n1 10Mb 1ms DropTail$ns duplex-link-op $n0 $r0 orient right$ns duplex-link-op $r0 $r1 orient right$ns duplex-link-op $r1 $n1 orient right$ns duplex-link-op $r0 $r1 queuePos 0.5#设定队列长度为18个封包大小set queue 18$ns queue-limit $r0 $r1 $queue#根据使用者的设定,指定TCP版本if

48、 $par1=Tahoe set tcp new Agent/TCP else set tcp new Agent/TCP/Reno$ns attach-agent $n0 $tcpset tcpsink new Agent/TCPSink$ns attach-agent $n1 $tcpsink$ns connect $tcp $tcpsink$tcp set fid_ 1#建立FTP应用程序set ftp new Application/FTP$ftp attach-agent $tcp#在0.0秒时,开始传送$ns at 0.0 $ftp start#在10.0秒时,结束传送$ns at

49、 10.0 $ftp stop#在0.0秒时去呼叫record来记录TCP的cwnd变化情况$ns at 0.0 record#在第10.0秒时去呼叫finish来结束模拟$ns at 10.0 finish#执行模拟$ns run附录BTCPV2代码if $argc !=1 puts Usage: ns lab12.tcl TCPversion puts Example:ns lab12.tcl Reno or ns lab12.tcl Newreno or ns lab12.tcl Sack exitset par1 lindex $argv 0#产生一个仿真的对象set ns new S

50、imulator#开启一个trace file,用来记录封包传送的过程set nd open out-$par1.tr w$ns trace-all $nd#开启一个档案用来记录cwnd变化情况set f0 open cwnd-$par1.tr w#定义一个结束的程序proc finish global ns nd f0 tcp par1#显示最后的平均吞吐量puts format average throughput: %.1f Kbps expr $tcp set ack_*($tcp set packetSize_)*8/1000.0/10$ns flush-trace#关闭档案clos

51、e $ndclose $f0#使用awk分析记录文件以观察队列的变化exec awk BEGIN highest_packet_id = -1;packet_count = 0;q_eln = 0;action = $1;time = $2;src_node = $3;dst_node = $4;type = $5;flow_id = $8;seq_no = $11;packet_id = $12;if (src_node = 0 & dst_node = 1) ( if (packet_id highest_packet_id) ( highest_packet_id = packet_id

52、;if (action = +) ( q_len+;print time, q_len;if (action = - | action = d) ( q_eln = q_len-;print time, q_len; out-$par1.tr queue_length-$par1.trexit 0#定义一个记录的程序#每格0.01秒就去记录当时的cwndproc record (global ns tcp f0set now $ns nowputs $f0 $now $tcp set cwnd_$ns at expr $now+0.01 record#产生传送节点,路由器r1,r2和接收节点s

53、et r0 $ns nodeset r1 $ns nodeset n0 $ns nodeset n1 $ns node#建立链路$ns duplex-link $n0 $r0 10Mb 1ms DropTail$ns duplex-link $r0 $r1 1Mb 4ms DropTail$ns duplex-link $r1 $n1 10Mb 1ms DropTail#设定队列长度为15个封包大小set buffer_size 15$ns queue-limit $r0 $r1 $buffer_size#根据使用者的设定,指定TCP版本if $par1=Reno set tcp new Ag

54、ent/TCP/Renoset tcpsink new Agent/TCPSink$tcp set debug_ 0 elseif $par1=Newreno set tcp new Agent/TCP/Newrenoset tcpsink new Agent/TCPSink$tcp set debug_ 0 else set tcp new Agent/TCP/Sack1set tcpsink new Agent/TCPSink/Sack1$tcp set debug_ 1$ns attach-agent $n0 $tcp#将awnd的值设为24,这是advertised window的上限

55、# advertised window是接收端的缓冲区可以容纳的封包个数,#因此当 congestion window 的值超过 advertised window 时,#TCP的传送端会执行流量控制以避免送的太快而导致接收端的缓冲区溢满。$tcp set window_ 24$ns attach-agent $n1 $tcpsink$ns connect $tcp $tcpsink#建立FTP应用程序set ftp new Application/FTP$ftp attach-agent $tcp#在0.0秒时,开始传送$ns at 0.0 $ftp start#在10.0秒时,结束传送$n

56、s at 10.0 $ftp stop#在0.0秒时去呼叫record来记录TCP的cwnd变化情况$ns at 0.0 record#在第10.0秒时去呼叫finish来结束模拟$ns at 10.0 finish#计算在传输路径上大约可以容纳多少的封包#计算方式:在bottleneck link上每秒可以传送的封包数*RTT+队列缓冲区大小puts format on path: %.2f packets expr (1000000/(8*($tcp set packetSize_+40) * (1+4+1) * 2 * 0.001) + $buffer_size#执行模拟$ns run附

57、录CTCPv3代码#产生一个仿真的对象set ns new Simulator#开启一个trace file,用来记录封包传送的过程set nd open out13_1.tr w$ns trace-all $nd#开启两个档案用来记录cwnd变化情况set f0 open cwnd0.tr wset f1 open cwnd1.tr w#定义一个结束的程序proc finish (global ns nd f0 tcp0 f1 tcp1#puts ACK number: $tcp0 set ack_puts format average throughput: %.1f Kbps expr

58、$tcp1 set ack_*($tcp1 set packetSize_)*8/1000.0/10puts format average throughput: %.1f Kbps expr $tcp0 set ack_*($tcp0 set packetSize_)*8/1000.0/10$ns flush-trace#关闭档案close $ndclose $f0close $f1#使用awk分析记录文件以观察队列的变化exec awk BEGIN highest_packet_id = -1;packet_count = 0;q_eln = 0;action = $1;time = $2

59、;src_node = $3;dst_node = $4;type = $5;flow_id = $8;seq_no = $11;packet_id = $12;if (src_node = 0 & dst_node = 1) ( if (packet_id highest_packet_id) ( highest_packet_id = packet_id;if (action = +) ( q_len+;print time, q_len;if (action = - | action = d) ( q_eln = q_len-;print time, q_len; out13_1.tr

60、queue_length-13_1.trexit 0#定义一个记录的程序#每格0.01秒就去记录当时的cwndproc record (global ns tcp0 f0 tcp1 f1set now $ns nowputs $f0 $now $tcp0 set cwnd_puts $f1 $now $tcp1 set cwnd_$ns at expr $now+0.01 record#建立节点set r0 $ns nodeset r1 $ns nodeset n0 $ns nodeset n1 $ns nodeset n2 $ns nodeset n3 $ns node#建立链路$ns du

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论