实验室要求利用NS-3模拟FTP传输数据流,查找资料发现可以参考的不多。
有一篇文章给了大概的思路,据此写了仿真代码,本人也不知道如何验证实现的是否正确。
首先是这篇论文:
Design of an Ns-3 Generic Application Architecture Applying Design Patterns
截取其中关于FTP的论述
仿真程序中,我设置了两个节点,一个是客户端,一个是FTP服务器。因为FTP有上传和下载的双向数据传输,因此,在两个节点都安装了发送应用和接受应用:
#include <string>
#include <fstream>
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/network-module.h"
#include "ns3/packet-sink.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("FTP_Traffic_two_paths_Example");
int
main (int argc, char *argv[])
{
bool tracing = false;
uint32_t maxBytes_1 = 5120000;
uint32_t maxBytes_2 = 2560000;
//
// Allow the user to override any of the defaults at
// run-time, via command-line arguments
//
CommandLine cmd;
cmd.AddValue ("tracing", "Flag to enable/disable tracing", tracing);
// cmd.AddValue ("maxBytes",
// "Total number of bytes for application to send", maxBytes_1);
cmd.Parse (argc, argv);
//
// Explicitly create the nodes required by the topology (shown above).
//
NS_LOG_INFO ("Create nodes.");
NodeContainer nodes;
nodes.Create (2);
NS_LOG_INFO ("Create channels.");
//
// Explicitly create the point-to-point link required by the topology (shown above).
//
PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5000Kbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("5ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
//
// Install the internet stack on the nodes
//
InternetStackHelper internet;
internet.Install (nodes);
//
// We've got the "hardware" in place. Now we need to add IP addresses.
//
NS_LOG_INFO ("Assign IP Addresses.");
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (devices);
NS_LOG_INFO ("Create Applications.");
//
// Create a BulkSendApplication and install it on node 0
//
uint16_t port_1 = 21; // well-known echo port number
BulkSendHelper source_1 ("ns3::TcpSocketFactory",
InetSocketAddress (i.GetAddress (1), port_1));
// Set the amount of data to send in bytes. Zero is unlimited.
source_1.SetAttribute ("MaxBytes", UintegerValue (maxBytes_1));
ApplicationContainer sourceApps_1 = source_1.Install (nodes.Get (0));
sourceApps_1.Start (Seconds (0.0));
sourceApps_1.Stop (Seconds (200.0));
uint16_t port_2 = 41; // well-known echo port number
BulkSendHelper source_2 ("ns3::TcpSocketFactory",
InetSocketAddress (i.GetAddress (0), port_2));
// Set the amount of data to send in bytes. Zero is unlimited.
source_2.SetAttribute ("MaxBytes", UintegerValue (maxBytes_2));
ApplicationContainer sourceApps_2 = source_2.Install (nodes.Get (1));
sourceApps_2.Start (Seconds (201.0));
sourceApps_2.Stop (Seconds (401.0));
//
// Create a PacketSinkApplication and install it on node 1
//
PacketSinkHelper sink_1 ("ns3::TcpSocketFactory",
InetSocketAddress (Ipv4Address::GetAny (), port_1));
ApplicationContainer sinkApps_1 = sink_1.Install (nodes.Get (1));
sinkApps_1.Start (Seconds (0.0));
sinkApps_1.Stop (Seconds (200.0));
PacketSinkHelper sink_2 ("ns3::TcpSocketFactory",
InetSocketAddress (Ipv4Address::GetAny (), port_2));
ApplicationContainer sinkApps_2 = sink_2.Install (nodes.Get (0));
sinkApps_2.Start (Seconds (201.0));
sinkApps_2.Stop (Seconds (401.0));
//
// Set up tracing if enabled
//
if (tracing)
{
AsciiTraceHelper ascii;
pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("FTP-TCP-send.tr"));
pointToPoint.EnablePcapAll ("FTP-TCP-send", false);
}
//
// Now, do the actual simulation.
//
NS_LOG_INFO ("Run Simulation.");
Simulator::Stop (Seconds (450.0));
Simulator::Run ();
Simulator::Destroy ();
NS_LOG_INFO ("Done.");
Ptr<PacketSink> sink1 = DynamicCast<PacketSink> (sinkApps_1.Get (0));
std::cout << "this first flow Total Bytes Received: " << sink1->GetTotalRx () << std::endl;
Ptr<PacketSink> sink2 = DynamicCast<PacketSink> (sinkApps_2.Get (0));
std::cout << "the second flow Total Bytes Received: " << sink2->GetTotalRx () << std::endl;
}
记录一下工作,不知对错