测试程序

#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"  
#include "ns3/random-variable-stream.h"
#include "ns3/random-variable-stream-helper.h"
using namespace ns3;  
using namespace std;  


NS_LOG_COMPONENT_DEFINE ("uplink-downlink-Example4");  


static void RxDrop (Ptr<const Packet> p)  //丢包 回调函数  
{  
  NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());  
}  


int  
main (int argc, char *argv[])  
{  
    Time::SetResolution (Time::NS);//设置时间单位为纳秒  
    LogComponentEnable ("uplink-downlink-Example4", LOG_LEVEL_INFO);  
    LogComponentEnable ("TcpL4Protocol", LOG_LEVEL_INFO);  
//    LogComponentEnable ("TcpSocketImpl", LOG_LEVEL_ALL);  
    LogComponentEnable ("PacketSink", LOG_LEVEL_INFO);  


//Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (1024));  
//Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("50Mb/s"));  
CommandLine cmd;  
cmd.Parse (argc,argv);  


NodeContainer nodes;  
nodes.Create (2);//创建2个节点 


PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("100000Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("0.002ms"));
NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);
InternetStackHelper stack;
stack.Install (nodes);


////////////设置丢包率 


Ptr<UniformRandomVariable> x =CreateObject<UniformRandomVariable>();
x->SetAttribute ("Min",DoubleValue(0));
x->SetAttribute ("Max",DoubleValue(1));


   // Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> ( "RanVar",  StringValue ("ns3::ConstantRandomVariable[Constant=0.000002583]"), "ErrorRate", DoubleValue (0.00001));  
    Ptr<RateErrorModel> em_1 = CreateObjectWithAttributes<RateErrorModel> ( "RanVar", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),"ErrorRate", DoubleValue (0.0001));
    devices.Get(0)->SetAttribute("ReceiveErrorModel", PointerValue (em_1));


    devices.Get(0)->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&RxDrop));  


   /////////////////////////////////////////////////////////////////////////////////////////////


    Ptr<RateErrorModel> em_2 = CreateObjectWithAttributes<RateErrorModel> ( "RanVar", StringValue("ns3::UniformRandomVariable[Min=0.0|Max=1.0]"),"ErrorRate", DoubleValue (0.0001));
    devices.Get(1)->SetAttribute("ReceiveErrorModel", PointerValue (em_2));


    devices.Get(1)->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&RxDrop));
   //////////////////////////////////////






Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign (devices);


uint16_t port_1 = 50000;  
ApplicationContainer sinkApp_1;  
Address sinkLocalAddress_1 (InetSocketAddress (Ipv4Address::GetAny (), port_1));  
PacketSinkHelper sinkHelper_1 ("ns3::TcpSocketFactory", sinkLocalAddress_1);  
sinkApp_1.Add(sinkHelper_1.Install(nodes.Get(1)));                // Packet sink 安装在第二个节点上


sinkApp_1.Start (Seconds (0.0));  
sinkApp_1.Stop (Seconds (30.0));  


OnOffHelper clientHelper_1 ("ns3::TcpSocketFactory", Address ());  


//clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ExponentialRandomVariable[Mean=0.01|Variance=0.5|Bound=0.0]")); 
clientHelper_1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0002583]"));


///注意是竖线分隔   各个不同的参数 
clientHelper_1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0001583]"));  


clientHelper_1.SetAttribute ("PacketSize", UintegerValue (1024));
clientHelper_1.SetAttribute ("DataRate", StringValue ("1Mbps"));


ApplicationContainer clientApps_1;  
//节点1>节点2  
AddressValue remoteAddress_1  
(InetSocketAddress (interfaces.GetAddress (1), port_1));  
clientHelper_1.SetAttribute("Remote",remoteAddress_1);  
clientApps_1.Add(clientHelper_1.Install(nodes.Get(0)));  




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




   /////////////////////////////////////downlink  ////////


uint16_t port_2 = 50001;
ApplicationContainer sinkApp_2;
Address sinkLocalAddress_2 (InetSocketAddress (Ipv4Address::GetAny (), port_2));
PacketSinkHelper sinkHelper_2 ("ns3::TcpSocketFactory", sinkLocalAddress_2);
sinkApp_2.Add(sinkHelper_2.Install(nodes.Get(0)));                // Packet sink 安装在第二个节点上


sinkApp_2.Start (Seconds (0.0));
sinkApp_2.Stop (Seconds (30.0));


OnOffHelper clientHelper_2 ("ns3::TcpSocketFactory", Address ());


//clientHelper.SetAttribute ("OffTime", StringValue ("ns3::ExponentialRandomVariable[Mean=0.01|Variance=0.5|Bound=0.0]")); 
clientHelper_2.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0002583]"));


///注意是竖线分隔   各个不同的参数 
clientHelper_2.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0001583]"));


clientHelper_2.SetAttribute ("PacketSize", UintegerValue (1024));
clientHelper_2.SetAttribute ("DataRate", StringValue ("100Mbps"));


ApplicationContainer clientApps_2;
//节点1>节点2  
AddressValue remoteAddress_2
(InetSocketAddress (interfaces.GetAddress (0), port_2));
clientHelper_2.SetAttribute("Remote",remoteAddress_2);
clientApps_2.Add(clientHelper_2.Install(nodes.Get(1)));




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








Ipv4GlobalRoutingHelper::PopulateRoutingTables ();  
//嗅探,记录所有节点相关的数据包  
pointToPoint.EnablePcapAll("uplink-downlink-Tcp4");  


Simulator::Run ();  
Simulator::Destroy ();  
return 0;  
}  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值