如图所示要实现n0到n5的TCP通信
每个链路之间的带宽都是100Mbps
/* -*- 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 ("BottleNeckTcpScriptExample"); int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); LogComponentEnable ("BottleNeckTcpScriptExample", LOG_LEVEL_INFO);
// LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_INFO);
LogComponentEnable ("PacketSink", LOG_LEVEL_ALL);
//LogComponentEnable ("OnOff", LOG_LEVEL_ALL); NodeContainer nodes;
nodes.Create (); NodeContainer n0n1 = NodeContainer(nodes.Get(),nodes.Get());
NodeContainer n1n2 = NodeContainer(nodes.Get(),nodes.Get());
NodeContainer n1n3 = NodeContainer(nodes.Get(),nodes.Get());
NodeContainer n3n4 = NodeContainer(nodes.Get(),nodes.Get());
NodeContainer n3n5 = NodeContainer(nodes.Get(),nodes.Get()); InternetStackHelper internet;
internet.Install(nodes); PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate",StringValue("100Mbps"));
p2p.SetChannelAttribute("Delay",StringValue("2ms")); NetDeviceContainer nets;
NetDeviceContainer d0d1 =p2p.Install(n0n1);
NetDeviceContainer d1d2 =p2p.Install(n1n2);
NetDeviceContainer d1d3 =p2p.Install(n1n3);
NetDeviceContainer d3d4 =p2p.Install(n3n4);
NetDeviceContainer d3d5 =p2p.Install(n3n5); Ipv4AddressHelper ipv4;
ipv4.SetBase("10.1.1.0","255.255.255.0");
Ipv4InterfaceContainer i0i1 = ipv4.Assign(d0d1); ipv4.SetBase("10.1.2.0","255.255.255.0");
ipv4.Assign(d1d2); ipv4.SetBase("10.1.3.0","255.255.255.0");
ipv4.Assign(d1d3); ipv4.SetBase("10.1.4.0","255.255.255.0");
ipv4.Assign(d3d4); ipv4.SetBase("10.1.5.0","255.255.255.0");
ipv4.Assign(d3d5);
Ipv4InterfaceContainer i3i5 = ipv4.Assign(d3d5); uint16_t port = ;
ApplicationContainer sinkApp;
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
sinkApp.Add(sinkHelper.Install(nodes.Get()));
sinkApp.Start (Seconds (0.0));
sinkApp.Stop (Seconds (10.0)); OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
clientHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); ApplicationContainer clientApps;
AddressValue remoteAddress(InetSocketAddress (i0i1.GetAddress (), port));
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodes.Get())); clientApps.Start(Seconds(1.0));
clientApps.Stop (Seconds (10.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
//嗅探,记录所有节点相关的数据包 Simulator::Run ();
Simulator::Destroy ();
return ;
}
1.首先针对拓扑的构造,对于这个拓扑构造个人觉得还是相对比较容易,通过对网上一些例子的理解,以及对上次myfirst.cc的理解
可参考 简单ns-3点对点网络的构造
NodeContainer nodes;
nodes.Create (6); NodeContainer n0n1 = NodeContainer(nodes.Get(0),nodes.Get(1));
NodeContainer n1n2 = NodeContainer(nodes.Get(1),nodes.Get(2));
NodeContainer n1n3 = NodeContainer(nodes.Get(1),nodes.Get(3));
NodeContainer n3n4 = NodeContainer(nodes.Get(3),nodes.Get(4));
NodeContainer n3n5 = NodeContainer(nodes.Get(3),nodes.Get(5)); InternetStackHelper internet;
internet.Install(nodes); PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate",StringValue("100Mbps"));
p2p.SetChannelAttribute("Delay",StringValue("2ms")); NetDeviceContainer nets;
NetDeviceContainer d0d1 =p2p.Install(n0n1);
NetDeviceContainer d1d2 =p2p.Install(n1n2);
NetDeviceContainer d1d3 =p2p.Install(n1n3);
NetDeviceContainer d3d4 =p2p.Install(n3n4);
NetDeviceContainer d3d5 =p2p.Install(n3n5); Ipv4AddressHelper ipv4;
ipv4.SetBase("10.1.1.0","255.255.255.0");
Ipv4InterfaceContainer i0i1 = ipv4.Assign(d0d1); ipv4.SetBase("10.1.2.0","255.255.255.0");
ipv4.Assign(d1d2); ipv4.SetBase("10.1.3.0","255.255.255.0");
ipv4.Assign(d1d3); ipv4.SetBase("10.1.4.0","255.255.255.0");
ipv4.Assign(d3d4); ipv4.SetBase("10.1.5.0","255.255.255.0");
ipv4.Assign(d3d5);
Ipv4InterfaceContainer i3i5 = ipv4.Assign(d3d5);
我的拓扑构建如上面所示,每两个点之间建立点对点链路,数据率100Mbps,信道传输延迟为2ms,然后对每一个网卡(设备),分配IP地址。
这个时候就有一个疑惑,网卡能分配两个以上的IP地址吗,就如上面的节点d1他被分配了10.1.1.2,又被分配了10.1.2.1的IP地址,这时候就想一个网卡能被分配两个以上的IP地址吗,(网络没学好。。) 答案是可以的。
然后再通过Ipv4InterfaceContainer获得等下我们所需的0号节点和5号节点的IP地址,进行TCP连接。
2.接下来是为0号节点装入TCP的sever,为5号节点安装tcp的client的application
uint16_t port = 50000;
ApplicationContainer sinkApp;
Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port)); //获得节点1的IP的地址以及端口号
PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress); //设置为TCP连接
sinkApp.Add(sinkHelper.Install(nodes.Get(0))); //把应用程序装入节点0
sinkApp.Start (Seconds (0.0));
sinkApp.Stop (Seconds (10.0)); OnOffHelper clientHelper ("ns3::TcpSocketFactory", Address ());
clientHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); ApplicationContainer clientApps;
AddressValue remoteAddress(InetSocketAddress (i0i1.GetAddress (0), port)); //设置远端IP以及端口,即服务器应用的IP和端口
clientHelper.SetAttribute("Remote",remoteAddress);
clientApps.Add(clientHelper.Install(nodes.Get(5))); //把应用程序装入节点5 clientApps.Start(Seconds(1.0));
clientApps.Stop (Seconds (10.0));
这是输出结果:
对于输出结果我存在很大的疑惑,为什么有时候传的是512字节,有时候是536字节,又或是112字节,当然绝大部分以512字节为主。
对于这个问题主要还是自己对TCP连接的原因理解的不够透彻,以及对一些模拟可视化的界面,还没办法弄出来加强理解。
在阅读相关文档后,还是安装TCP应用的地方存在疑惑。 接下来应该学习一些如何操作输出结果,看到可视化的一些东西,以加强对各种数据传输的理解。