NS3中second.cc代码解读

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
	 * published by the Free Software Foundation;
 *
	 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"

 // Default Network Topology
 //
 //       10.1.1.0
 // n0 -------------- n1   n2   n3   n4   n5    n6
 //    point-to-point  |    |    |    |    |     |
 //                    ============================
 //                         LAN 10.1.2.0
//点对点和局域网

using namespace ns3;

//声明了一个叫NetWorkHomeWork的日志构件,通过引用NetWorkHomeWork这个名字的操作,
//可以实现打开或者关闭控制台日志的输出。
NS_LOG_COMPONENT_DEFINE("NetWorkHomeWork");//日志

int
main(int argc, char* argv[])
{   
//定义变量,用于决定是否开启两个UdpApplication的Logging组件;默认true开启
bool verbose = true;
//局域网节点数量
uint32_t nCsma = 4;//我是节点数修改我。

//命令行参数,允许用户指定脚本运行时的参数
CommandLine cmd;
cmd.AddValue("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma);
//命令行参数设置是否开启logging
cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse(argc, argv);

//根据用户命令行参数,启用Logging
if (verbose)
{
    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
}


/**************开始定义网络拓扑***************/
//两个p2p节点:n0和n1

NodeContainer p2pNodes;
p2pNodes.Create(2);

//创建另一个NodeContainer类对象,用于总线(CSMA)网络,总线型局域网
//4个CSMA节点
NodeContainer csmaNodes;
csmaNodes.Add(p2pNodes.Get(1));//把n1节点添加到局域网
//再创建5个节点
csmaNodes.Create(nCsma);

//对链路进行配置
//-----------------------------------------------------------------------------------
//P2P链路属性
//设置传送速率和信道延迟
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));//
pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));

NetDeviceContainer p2pDevices;
p2pDevices = pointToPoint.Install(p2pNodes);//将链路安装到节点

//总线网络属性
 //创建和连接CSMA设备及信道
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));//纳秒

NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
//------------------------------------------------------------------------------



//安装协议栈
InternetStackHelper stack;
//P2P链路中的第一个节点
stack.Install(p2pNodes.Get(0));
stack.Install(csmaNodes);

//分配IP地址
 //安排P2P网段的地址
Ipv4AddressHelper address;
address.SetBase("10.1.1.0", "255.255.255.0");   //p2p网段
Ipv4InterfaceContainer p2pInterfaces;
p2pInterfaces = address.Assign(p2pDevices);
//安排CSMA网段地址
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));//在1.0秒开始
serverApps.Stop(Seconds(10.0));//在10.0秒结束

UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(nCsma), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(4));//发送包的次数
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));//间隔
echoClient.SetAttribute("PacketSize", UintegerValue(1024));//包的大小

//将Client服务安装在P2P网段的第一个节点上
ApplicationContainer clientApps = echoClient.Install(p2pNodes.Get(0));
clientApps.Start(Seconds(2.0));//在2.0秒开始
clientApps.Stop(Seconds(10.0));//在10.0秒开始
/**************应用程序结束***************/


//全局路由管理器根据节点产生的链路通告为每个节点建立路由表
Ipv4GlobalRoutingHelper::PopulateRoutingTables();


/****************开启pcap跟踪*******************/
//开启P2PHelper类对象的pcap
//开启pcap跟踪,生成以"homeWork"为前缀的文件名
pointToPoint.EnablePcapAll("homeWork");


//开启csmaHelper类对象的pcap
//使用csma网段第二个节点进行sniff,True开启混杂模式
csma.EnablePcap("homeWork", csmaDevices.Get(1), true);

Simulator::Run();
Simulator::Destroy();

return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值