ns3自带应用层协议如下:
应用层的开关模型,可以设置开时间和关时间,在开时间以固定的码率产生数据,在关时间不产生数据,只有在开时间可以产生数据和发送分组,具体请看下面分析。
除此之外OnOffApplication还有另外3个常用的属性:
- PacketSize:发送分组负载大小,单位是B
- DataRate:分组产生速率,单位可指定不固定
- MaxBytes:能够发送的最大字节总数,默认值是0,即没有发送字节数上限。
OnOffApplication即支持TCP也支持UDP,对应的服务端应用为PacketSink
以下示例代码在example/tcp/tcp-star-server.cc文件中,拷贝到scratch文件夹下即可运行。拓扑是一个星形拓扑,中间节点运行服务端程序(PacketSink),其他节点运行客户机程序,也就是开关模型
// Default Network topology, 9 nodes in a star
/*
n2 n3 n4
\ | /
\|/
n1---n0---n5
/| \
/ | \
n8 n7 n6
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#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"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TcpServer");
void MacTxCallback(std::string context,Ptr<const Packet> packet){
NS_LOG_UNCOND("time:"<<Simulator::Now().GetSeconds()<<" size:"<<packet->GetSize()<<" "<<context);
}
void MacRxCallback(std::string context,Ptr<const Packet> packet){
NS_LOG_UNCOND("time:"<<Simulator::Now().GetSeconds()<<" size:"<<packet->GetSize()<<" "<<context);
}
void RxPacketCall(std::string context,Ptr<const Packet> packet, const Address &address){
NS_LOG_UNCOND ("At time " << Simulator::Now ().GetSeconds ()
<< "s packet sink received "
<< packet->GetSize () << " bytes from "
<< InetSocketAddress::ConvertFrom(address).GetIpv4 ()
<< " port " << InetSocketAddress::ConvertFrom (address).GetPort ()
);
}
int
main (int argc, char *argv[])
{
//LogComponentEnable ("TcpServer", LOG_LEVEL_INFO);
//LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_ALL);
//LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);
//LogComponentEnable ("PacketSink", LOG_LEVEL_INFO);
LogComponentEnable ("OnOffApplication", LOG_LEVEL_INFO);
//LogComponentEnable ("Ipv4ListRouting", LOG_LEVEL_INFO);
// Set up some default values for the simulation.
Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (250));//单位是B=8bit?
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("5kb/s"));//大概是0.4s发送一个分组,分组大小是250b吗?
uint32_t N = 3; //number of nodes in the star
// Allow the user to override any of the defaults and the above
// Config::SetDefault()s at run-time, via command-line arguments
CommandLine cmd;
cmd.AddValue ("nNodes", "Number of nodes to place in the star", N);
cmd.Parse (argc, argv);
// Here, we will create N nodes in a star.
NS_LOG_INFO ("Create nodes.");
NodeContainer serverNode;
NodeContainer clientNodes;
serverNode.Create (1);
clientNodes.Create (N-1);
NodeContainer allNodes = NodeContainer (serverNode, clientNodes);
// Install network stacks on the nodes
InternetStackHelper internet;
internet.Install (allNodes);
//Collect an adjacency list of nodes for the p2p topology
std::vector<NodeContainer> nodeAdjacencyList (N-1);
for(uint32_t i=0; i<nodeAdjacencyList.size (); ++i)
{
nodeAdjacencyList[i] =