ns-3 first.cc

该示例脚本演示了如何在ns-3中创建一个简单的网络,包含两个节点,通过PPP协议进行通信。脚本配置了5Mbps的链路速率和2ms的延迟,并使用UDP Echo Server和Client应用进行数据传输。客户端在2秒后开始发送1KB大小的包,每秒发送一次,持续到10秒。
摘要由CSDN通过智能技术生成

示例脚本:【examples/tutorial/first.cc】
模拟了一个有线网络,包含两个结点
链路层使用PPP传输分组
在这里插入图片描述

/* -*- 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"			// 定义了ns-3的核心功能(如模拟事件、事件调度),必须包括
#include "ns3/network-module.h"			// 定义了ns-3的基本网络组件(如网络结点、分组和地址等),必须包括
#include "ns3/internet-module.h"		// 定义了TCP/IP协议栈
#include "ns3/point-to-point-module.h"	//
#include "ns3/applications-module.h"	// 定义了应用层的分组收发模型(如贪婪模型、ON/OFF模型等)

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *argv[])
{
  CommandLine cmd;
  cmd.Parse (argc, argv);												// 读取命令行参数
  
  Time::SetResolution (Time::NS);										// 设置最小模拟时间单元:ns
  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);		// 开启Log组件,打印指定Log组件信息
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

  NodeContainer nodes;
  nodes.Create (2);														// 创建网络结点

  PointToPointHelper pointToPoint;										// PPP信道助手类
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));	// 配置PPP信道传输速率属性
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));		// 配置PPP信道传播延迟属性

  NetDeviceContainer devices;											// 创建网络设备
  devices = pointToPoint.Install (nodes);								// 连接结点与信道

  InternetStackHelper stack;
  stack.Install (nodes);												// 为nodes容器中的结点安装TCP/IP协议栈

  Ipv4AddressHelper address;											// 为网络设备分配IP地址
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  UdpEchoServerHelper echoServer (9);									// 监听9号端口

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));	// 在结点1中安装服务端程序
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);		// 配置客户端程序属性
  echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));	// 在结点0中安装客户端程序
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itsdandy

感谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值