2014-11-22 56 views
0

我試圖創建一個非常簡單的拓撲,其中包含通過路由器連接的服務器和客戶端。下面的代碼是通過GUI獲得的。我正在嘗試運行它,但它引發了我對ns-3的一個新的錯誤,所以請耐心等待。在ns-3中使用路由器創建拓撲

#include "ns3/simulator-module.h" 
#include "ns3/node-module.h" 
#include "ns3/core-module.h" 
#include "ns3/common-module.h" 
#include "ns3/global-route-manager.h" 
#include "ns3/helper-module.h" 
#include "ns3/bridge-module.h" 

using namespace ns3; 

int main(int argc, char *argv[]) 
{ 
CommandLine cmd; 
cmd.Parse (argc, argv); 

/* Configuration. */ 

/* Build nodes. */ 
NodeContainer term_0; 
term_0.Create (1); 
NodeContainer term_1; 
term_1.Create (1); 
NodeContainer router_0; 
router_0.Create (1); 

/* Build link. */ 
CsmaHelper csma_hub_0; 
csma_hub_0.SetChannelAttribute ("DataRate", DataRateValue (100000000)); 
csma_hub_0.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000))); 
CsmaHelper csma_hub_1; 
csma_hub_1.SetChannelAttribute ("DataRate", DataRateValue (100000000)); 
csma_hub_1.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10000))); 

/* Build link net device container. */ 
NodeContainer all_hub_0; 
all_hub_0.Add (router_0); 
all_hub_0.Add (term_0); 
NetDeviceContainer ndc_hub_0 = csma_hub_0.Install (all_hub_0); 
NodeContainer all_hub_1; 
all_hub_1.Add (router_0); 
all_hub_1.Add (term_1); 
NetDeviceContainer ndc_hub_1 = csma_hub_1.Install (all_hub_1); 

/* Install the IP stack. */ 
InternetStackHelper internetStackH; 
internetStackH.Install (term_0); 
internetStackH.Install (term_1); 
internetStackH.Install (router_0); 

/* IP assign. */ 
Ipv4AddressHelper ipv4; 
ipv4.SetBase ("10.0.0.0", "255.255.255.0"); 
Ipv4InterfaceContainer iface_ndc_hub_0 = ipv4.Assign (ndc_hub_0); 
ipv4.SetBase ("10.0.1.0", "255.255.255.0"); 
Ipv4InterfaceContainer iface_ndc_hub_1 = ipv4.Assign (ndc_hub_1); 

/* Generate Route. */ 
Ipv4GlobalRoutingHelper::PopulateRoutingTables(); 

/* Generate Application. */ 
uint16_t port_udpEcho_0 = 9; 
UdpEchoServerHelper server_udpEcho_0 (port_udpEcho_0); 
ApplicationContainer apps_udpEcho_0 = server_udpEcho_0.Install (term_1.Get(0)); 
apps_udpEcho_0.Start (Seconds (0.0)); 
apps_udpEcho_0.Stop (Seconds (2.0)); 
Time interPacketInterval_udpEcho_0 = Seconds (1.0); 
UdpEchoClientHelper client_udpEcho_0 (iface_ndc_hub_1.GetAddress(1), 9); 
client_udpEcho_0.SetAttribute ("MaxPackets", UintegerValue (1)); 
client_udpEcho_0.SetAttribute ("Interval", TimeValue (interPacketInterval_udpEcho_0)); 
client_udpEcho_0.SetAttribute ("PacketSize", UintegerValue (1024)); 
apps_udpEcho_0 = client_udpEcho_0.Install (term_0.Get (0)); 
apps_udpEcho_0.Start (Seconds (0.1)); 
apps_udpEcho_0.Stop (Seconds (2.0)); 

/* Simulation. */ 
/* Pcap output. */ 
/* Stop the simulation after x seconds. */ 
uint32_t stopTime = 3; 
Simulator::Stop (Seconds (stopTime)); 
/* Start and clean simulation. */ 
Simulator::Run(); 
Simulator::Destroy(); 
} 

回答

2

如果你是新手,你應該通過查看已經由ns-3提供的示例來啓動你的程序。像這樣:https://www.nsnam.org/doxygen/simple-routing-ping6_8cc_source.html。看網絡拓撲結構,這是你想要的。

試圖運行你的代碼,我不得不改變很多頭文件來編譯它(可能是不同的版本,或者你的GUI過時了)。刪除你的,並把這些:

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

最後,你什麼都沒有打印,所以我不知道你在做什麼。正如你所使用的,UdpEchoClientHelper和UdpEchoServerHelper,我會建議啓用他們的日誌。你可以用這兩行來做到這一點:

/* Configuration. */ 
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); 
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); 

我也是新的,我不知道你在做什麼。所以我會在這裏停止我的回答。祝你好運。