ns3 myfirst.cc 两个节点点对点通信

首先在ns3.25/examples/tutorial/下找到 first.cc文件,将他拷贝到到scratch目录下。

 

然后为了方便将代码打出来

/* -*- 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/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

int
main (int argc, char *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);

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (2));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
  clientApps.Start (Seconds (2.0));
  clientApps.Stop (Seconds (10.0));

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

 

首先研究头文件

#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"

 这些头文件都被存放在build目录下的一个ns3的目录下,我们可以根据自己所需要的功能引用相应的头文件进来

 

using namespace ns3;

 ns3的命名空间,这样我们就可以不用再写 ns3::xxx ,否则会编译错误。

 

NS_LOG_COMPONENT_DEFINE ("FirstScriptExample");

 日志组件,这句话一看上去有点蒙,实际上的功能就是 生成一个名字为“FirstScriptExample"的日志组件。

 

Time::SetResolution (Time::NS);

 设置时间单位为纳秒,虽然我不是很明白为什么要设置时间单位为纳秒??这个是百度得到的 存在疑点

 

  LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);

 是两个日志组件生效,并且日志级别设置为INFO,这个两个日志是内建在Echo Client和Echo Sever的应用中。等下会有用到。

 

  NodeContainer nodes;
  nodes.Create (2);

 这句话差不多就是 声明一个节点容器 nodes,然后在李勇Create(2)生成两个节点。

 

  PointToPointHelper pointToPoint;
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

 这个PointToPointHelper类负责来设置网络设备和信道属性。

其中把网络设备中“DateRate"数据速率设置为5Mbps,信道属性的延迟设置为2ms.  这个等下会在后面的生成结果,以及自己所学网络知识解释一下他用在什么地方。

 

  NetDeviceContainer devices;
  devices = pointToPoint.Install (nodes);

 前面已经完成了用NodeContainer来创建节点,用PointToPointHelper来做关于创建、配置、安装的工作。现在他们两个之间执需要连接起来。需要一个NetDevices对象列表,可以通过NetDeviceContainer来存放他们,就像使用第一个NodeContainer对象来存放自身所创建的节点一样。

PointToPointHelper的Install()方法一个NodeContainer对象作为一个参数,以一个NetDevice Container为返回对象。

对于NodeContainer的每一个节点,因为这里建立的是点对点的信道,所以明确的2个节点。一个PointToPointChannel对象被创建,2个PointToPointNetDevices和他连接。

所以最终就得到了,两个节点,每个节点安装了点到点网络设备,这两个网络设备之间安装了点到点的信道,2个设备会被配置在一个有2ms传输时延的信道上以5Mbps的速率传送数据。

 

  InternetStackHelper stack;
  stack.Install (nodes);

 InternetStackHelper会为NodeContainer的每一个Node安装一个网络协议。

 

 Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

 Ipv4AddressHelper为节点上的设备设置IP地址。通过SetBase方法告诉它从10.1.1.0开始以子网掩码为255.255.255.0分配地址,地址分配默认是从1开始单调增长。

 

 

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

 这个代码完成了地址配置, ns3中使用Ipv4Interface对象将一个IP地址同一个设备关联起来。而Ipv4InterfaceContainer提供了这样的功能,作为Ipv4Interface对象的容器。

 

到这里已经完成了一个 安装了了协议栈,配置了IP地址类的点到点的网络。 现在就是要用它做数据通信。

 

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (1));
  serverApps.Start (Seconds (1.0));
  serverApps.Stop (Seconds (10.0));

 

声明了一个UdpEchoServerHelper 像其他Help类一样这个也不是应用本身,而是一个用来创建真正应用的对象,其中端口为9号端口。

并在刚刚NodeContainer里面的2号节点 这里0为1号节点,1为2号节点。装入服务器应用,同样的这个的返回是一个Application对象,需要一个ApplicationContainer作为容器来存储

应用对象需要一个时间参数 ,开始以及停止 ,这里两行使得echo服务应用在1s时开始,在10s的时候停止。

 

 UdpEchoClientHelper echoClient (interfaces.GetAddress (1), 9);
  echoClient.SetAttribute ("MaxPackets", UintegerValue (2));
  echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
  echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

 同样的客户端应用,首先给客户端的 UdpEchoClientHelper类 传入要客户端要发送数据到的IP地址,以及端口号,这里设置的是服务器的设备IP地址,以及刚刚设置接受9号端口。

MaxPackets为在模拟期间能发送的最多的分组个数

Interval 为两个分组的要等待多少时间

PacketSize 为每个分组承载多少数据。 这里的参数1024是字节,不是位!!!

 

ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

 

 然后再用同样的方法,把一号节点作为客户端,把客户端应用装在一号节点上,

应用开始服务为2s,结束服务为10s

至于为什么是2s开始,我也是有点蒙,查了有关资料,说是要等服务器生效1s后才开始作为服务比较合理。也可以通过改变上面参数为1,这是客户端就变成了1s的时候开始服务,就是1s时候开始发送数据。

 

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

 启动模拟器,Simulator::Run(),系统会开始遍历预设事件的列表并执行。

执行完毕用,就调用Simulator::Destory()来销毁刚刚所创建的一系列东西。

 

在eclipse下执行

 

运行结果

这是发送一个包,即客户端发送一个1024字节的分组到服务器,而这里客户端的端口我觉得应该是随机设定的,因为从头到尾就没有碰到过49153端口。

而为什么是 2.00369s才收到 我做了一个运算 即在2s的基础上  2s+1024*8/(5*10^6)(发送时延)s+0.002s(传输时延)=2.0036384s 与20.00369s还有一定差距,可差距已经很小,所以个人觉得把那部分时延可以视为处理时延(这里不存在排队时延).

转载于:https://www.cnblogs.com/wpqwpq/p/5671177.html

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验内容1. 在上一个实验的基础上,建立ad hoc节点拓扑模型,网络节点均具有路由转发功能; 2. 进入~/tarballs/ns-allinone-3.2x/ns-3.2x/examples/wireless$ 目录,在wireless文件夹下,有很多wifi传输的示例。在wifi-simple-adhoc-grid.cc代码基础上修改,文件拷贝到scratch文件夹下,重命名为你的姓名缩写+学号,例如学生姓名钢铁侠,学号20191314,c文件命名为gtx20191314.cc; 3. 读懂wifi-simple-adhoc-grid.cc代码,掌握在NS-3中网络拓扑布局,ad hoc传输方式、网络路由的简单配置等,成功运行wifi-simple-adhoc-grid.cc; 4. 掌握拓扑建模方法,在wifi-simple-adhoc-grid.cc代码中扩展建立到100个节点节点初始呈现网格布局; 5. 深入研究移动模型建模方法,完成对移动模型参数设置,使得节点移动在可视化界面更加明显; 6. 深入研究信道模型建模方法,在代码中完成对信道模型的替换,例如Friis、LogDistancePropagationLossModel信道模型。通过射频参数,了解发射机、路径损耗和接收机间增益的计算方法。可以根据参数配置想要的发射距离; 7. 掌握网路层路由配置方法,可以配置多种ad hoc路由模式,例如OLSR、aodv等。查阅不同路由协议的工作原理和方法; 8. 掌握统计模型的使用方法,可以统计相应节点的时延、丢包率等重要指标; 9. 掌握能耗模型的使用方法,配置节点能量,设备能量,可以统计节点的能量消耗; 10. 掌握统计数据作图方法,对于获得的统计数据,图形化的形式得出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值