Ns3 構建啞鈴型拓撲,並實現兩個點的TCP連接(詳細請戳全文)


如圖所示要實現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 (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);

  uint16_t port = 50000;
  ApplicationContainer sinkApp;
  Address sinkLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", sinkLocalAddress);
  sinkApp.Add(sinkHelper.Install(nodes.Get(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));
  clientHelper.SetAttribute("Remote",remoteAddress);
  clientApps.Add(clientHelper.Install(nodes.Get(5)));

  clientApps.Start(Seconds(1.0));
  clientApps.Stop (Seconds (10.0));

  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  //嗅探,記錄所有節點相關的數據包


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

 

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應用的地方存在疑惑。 接下來應該學習一些如何操作輸出結果,看到可視化的一些東西,以加強對各種數據傳輸的理解。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM