first.cc
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
// 默认的网络拓扑
//
// 10.1.1.0
// n0 -------------- n1
// point-to-point
//
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("FirstScriptExample");
int
main(int argc, char* argv[])
{
CommandLine cmd(__FILE__);
cmd.Parse(argc, argv);
//设置时间单位为纳秒
Time::SetResolution(Time::NS);
//日志输出
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
//创立两个节点
NodeContainer nodes;
nodes.Create(2);
//点对点的信道
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
//网络设备
NetDeviceContainer devices;
devices = pointToPoint.Install(nodes);
//协议栈
InternetStackHelper stack;
stack.Install(nodes);
//设置IPv4地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
//将地址分配给网络设备
Ipv4InterfaceContainer interfaces = address.Assign(devices);
//UDP服务器的端口设置为9
UdpEchoServerHelper echoServer(9);
//服务器应用设置在n1节点
ApplicationContainer serverApps = echoServer.Install(nodes.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
//UDP客户端设置好 **远端服务器的** IP和端口 以及 链接的参数
UdpEchoClientHelper echoClient(interfaces.GetAddress(1), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
//客户端应用设置在n0节点
ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
//仿真启动
Simulator::Run();
Simulator::Destroy();
return 0;
}
总的来说,仿真流程如下:
- 创立节点
- 创立信道
- 创立网络设备
- 节点安装协议栈
- 将IP地址分配给网络设备
- 设置服务器应用
a) 服务器端口
b) 服务器应用所在节点 - 设置客户端应用
a) 远端服务器IP及端口
b) 链接的参数
c) 客户端应用所在节点 - 启动仿真
second.cc
#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
// 默认的网络拓扑
//
// 10.1.1.0
// n0 -------------- n1 n2 n3 n4
// point-to-point | | | |
// ================
// LAN 10.1.2.0
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("SecondScriptExample");
int
main(int argc, char* argv[])
{
//CMD输入参数的设置代码
bool verbose = true;
uint32_t nCsma = 3;
CommandLine cmd(__FILE__);
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse(argc, argv);
if (verbose)
{
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}
nCsma = nCsma == 0 ? 1 : nCsma;
//创立节点
NodeContainer p2pNodes;
p2pNodes.Create(2);
NodeContainer csmaNodes;
csmaNodes.Add(p2pNodes.Get(1));
csmaNodes.Create(nCsma);
//p2p信道
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
//p2p设备
NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install(p2pNodes);
//csma信道
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
//csma设备
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
//安装协议栈
InternetStackHelper stack;
stack.Install(p2pNodes.Get(0));
stack.Install(csmaNodes);
//分配IP地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign(p2pDevices);
address.SetBase("10.1.2.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = address.Assign(csmaDevices);
//服务器配置
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(nCsma));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
//客户端配置
UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(1));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
//建立路由数据库并初始化模拟中节点的路由表。将模拟中的所有节点制作成路由器。
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
//输出pcap文件,可以使用WireShark打开分析
pointToPoint.EnablePcapAll("second");
csma.EnablePcap("second", csmaDevices.Get(1), true);
//启动仿真
Simulator::Run();
Simulator::Destroy();
return 0;
}