首页 > 编程知识 正文

ns流程图循环结构,ns3官网

时间:2023-05-05 09:01:19 阅读:166534 作者:3394

提示:写完文章后,目录可以自动生成。 如何生成可以参考右侧的帮助文档

ns3入门教程<; 3>; 首先,硬件实现2、通信实现1、传输层UDP通信的实现2、代码分析3、总结

首先在上次报告之后,实现了星型结构网络环境并进行了仿真

另一方面,硬件实现在硬件实现方面需要查看ns3文件夹下的/usr/local/workspace/ns-allinone-3.28.1/ns-3.28.1/examples/tutorial文件夹下的文件:

first.cc、second.cc、third.cc等文件对ns3的硬件模拟有很好的认识。这里就不多讲了,直接坐上星型结构的仿真代码进行分析。

///network topology///n0 n1 N2 n3/||//-------------------/switch//----------------------------------- - ---------------------------1-2-1-2-1-2-2-2-1- 1 - tracingofqueuesandpacketreceptionstofile ' csma-bridge.tr ' # include iostream # include ' para.h ' # include ' ns3/CS3 include ' ns3/applicatation include ' ns3/csma-module.h ' # include ' ns3/internet-module.h ' # include ' ns3/ns3 intmain(intargc,char *argv[] () (//usersmayfinditconvenienttoturnonexplicitdebugging//forselectedmodules; thebelowlinessuggesthowtodothis//command line cmd; cmd.parse(argc,argv ); ns_log_info('createnodes.' ); //设定通信节点的数量,共计8个通信节点NodeContainer terminals; terminals.create(8; //构建网桥,即网络中心node container csma交换机; CSMAswitch.create(1; //构建桥接器,信道的带宽为10Mbps,信道延迟为2 msns _ log _ info (构建拓扑); CsmaHelper csma; csma.setchannelattribute (' datarate ',dataratevalue(1000000 ) ); csma.setchannelattribute (' delay ',timevalue ) milliseconds ) ); 在//Create the csma links,from each terminal to the switch //桥接器上安装通信设备netdevicecontainerterminaldevices; netdevicecontainerswitchdevices; //连接网桥和通信节点的for(intI=0; i 8; I ) { netdevicecontainerlink=csma.install (node container (terminals.get (I ),csmaSwitch ) }; terminaldevices.add(link.get(0) ); switchdevices.add(link.get(1) ); } //Create the bridge netdevice,whichwilldothepacketswitchingptrnodeswitchnode=csma switch.get (0); BridgeHelper bridge; bridge.Install(switchnode,switchDevices ); //addinternetstacktotheterminalsinternets

tackHelper internet; internet.Install (terminals); // We've got the "hardware" in place. Now we need to add IP addresses. // NS_LOG_INFO ("Assign IP Addresses."); Ipv4AddressHelper ipv4; ipv4.SetBase ("10.1.1.0", "255.255.255.0"); ipv4.Assign (terminalDevices);

到此,基于星型结构的网络仿真已经实现到网络层,只需要在其上添加其传输层的协议,就可以实现网络仿真。

二、通信实现 1.传输层UDP通信实现 uint16_t port = 9; // Discard port (RFC 863) // Create a similar flow from n0 to n7, starting at time 1.0 seconds //这里的IP地址是指定收的节点的IP地址 OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address ("10.1.1.8"), port))); onoff.SetConstantRate (DataRate ("4Mb/s")); //set node datarate ApplicationContainer app = onoff.Install (csmaNodes.Get (0)); // Start the application app.Start (Seconds (1.0)); app.Stop (Seconds (10.0)); // Create an optional packet sink to receive these packets PacketSinkHelper sink ("ns3::UdpSocketFactory", Address (InetSocketAddress (Ipv4Address::GetAny (), port))); app = sink.Install (csmaNodes.Get (7)); app.Start (Seconds (0.0)); // Create a similar flow from n1 to n6, starting at time 1.0 seconds onoff.SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.7"), port))); app = onoff.Install (csmaNodes.Get (1)); app.Start (Seconds (1)); app.Stop (Seconds (10.0)); app = sink.Install (csmaNodes.Get (6)); app.Start (Seconds (0.0)); // Create a similar flow from n2 to n5, starting at time 1.0 seconds onoff.SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.6"), port))); app = onoff.Install (csmaNodes.Get (2)); app.Start (Seconds (1)); app.Stop (Seconds (10.0)); app = sink.Install (csmaNodes.Get (5)); app.Start (Seconds (0.0)); // Create a similar flow from n3 to n4, starting at time 1.0 seconds onoff.SetAttribute ("Remote", AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.5"), port))); app = onoff.Install (csmaNodes.Get (3)); app.Start (Seconds (1)); app.Stop (Seconds (10.0)); app = sink.Install (csmaNodes.Get (4)); app.Start (Seconds (0.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); //抓取网络通信过程中的通信包 csma.EnablePcapAll ("CSAM_4pair_4Mbps_UDP", false); FlowMonitorHelper flowmon; flowmon.InstallAll (); Simulator::Stop (Seconds (10)); Packet::EnablePrinting (); //将网络仿真过程可视化,并且设置网络节点的位置。 AnimationInterface anim("CSAM_4pair_4Mbps_UDP.xml"); anim.SetConstantPosition(csmaNodes.Get(0),10,10); anim.SetConstantPosition(csmaNodes.Get(1),10,20); anim.SetConstantPosition(csmaNodes.Get(2),10,30); anim.SetConstantPosition(csmaNodes.Get(3),20,10); anim.SetConstantPosition(csmaNodes.Get(4),20,20); anim.SetConstantPosition(csmaNodes.Get(5),20,30); anim.SetConstantPosition(csmaNodes.Get(6),30,10); anim.SetConstantPosition(csmaNodes.Get(7),30,20); //仿真开始 Simulator::Run (); //仿真流量监控 flowmon.SerializeToXmlFile ("CSAM_4pair_4Mbps_UDP.flowmon", true, true); //仿真结束 Simulator::Destroy (); return 0; 2.代码分析

2.1 其实在NS3中有很多可以实现UDP的类,但是为什么偏偏选择使用onoff类呢,主要有以下两个个原因:

可以控制流量,可以设置发送速率的大小,满足题目的要求。便于UDP和TCP之间的转化,只需要修改 “ns3::UdpSocketFactory” 为**“ns3::TcpSocketFactory”** 就可以实现传输层协议的更换。

2.2 其次有基于 netanim 的仿真实现,其动态效果如下所示。

三、 总结

实验的全部代码可到我的资源中心下载

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。