在UDP P2P协议中,服务器实例没有响应

时间:2018-12-07 05:57:39

标签: udp client ns2 ns-3

该代码主要试图在两个不同的端口上创建两个服务器实例,同时也在两个客户端上创建实例。每个客户端都与专用服务器建立p2p连接。不考虑并发通信。代码中的问题是服务器节点没有响应。我是ns3的初学者。这个问题可能很琐碎。请建议我解决问题的方法。另外,我还使用Flow Monitor来测量吞吐量。

int main(int argc, char *argv[])
{
    CommandLine cmd;
    cmd.Parse(argc, argv);
    Time::SetResolution(Time::NS);
    LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
    LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);

    NodeContainer nodes1,nodes2;
    nodes1.Create(2);
    nodes2.Create(2);

    PointToPointHelper pointToPoint1,pointToPoint2;
    pointToPoint1.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
    pointToPoint1.SetChannelAttribute("Delay", StringValue("2ms"));
    pointToPoint2.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
    pointToPoint2.SetChannelAttribute("Delay", StringValue("2ms"));

    NetDeviceContainer devices1,devices2;
    devices1 = pointToPoint1.Install(nodes1);
    devices2 = pointToPoint2.Install(nodes2);


    InternetStackHelper stack1,stack2;
    stack1.Install(nodes1);//
    stack2.Install(nodes2);

    Ipv4AddressHelper address1;
    address1.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4AddressHelper address2;
    address2.SetBase("10.5.4.0", "255.255.255.0");

    Ipv4InterfaceContainer interfaces1 = address1.Assign(devices1);
    Ipv4InterfaceContainer interfaces2 = address2.Assign(devices2);

    UdpEchoServerHelper echoServer1(8889),echoServer2(8850);

    ApplicationContainer serverApps1 = echoServer1.Install(nodes1.Get(0));
    ApplicationContainer serverApps2 = echoServer2.Install(nodes2.Get(0));
    serverApps1.Start(Seconds(1.0));
    serverApps1.Stop(Seconds(10.0));
    serverApps2.Start(Seconds(1.0));
    serverApps2.Stop(Seconds(10.0));

    UdpEchoClientHelper echoClient1(interfaces1.GetAddress(1), 8889),echoClient2(interfaces2.GetAddress(1), 8850);
    echoClient1.SetAttribute("MaxPackets", UintegerValue(1));
    echoClient1.SetAttribute("Interval", TimeValue(Seconds(1.0)));
    echoClient1.SetAttribute("PacketSize", UintegerValue(1024));
    echoClient2.SetAttribute("MaxPackets", UintegerValue(1));
    echoClient2.SetAttribute("Interval", TimeValue(Seconds(1.0)));
    echoClient2.SetAttribute("PacketSize", UintegerValue(1024));


    ApplicationContainer clientApps1 = echoClient1.Install (nodes1.Get (1));
    clientApps1.Start (Seconds (2.0));
    clientApps1.Stop (Seconds (10.0));
    ApplicationContainer clientApps2 = echoClient2.Install (nodes2.Get (1));
    clientApps2.Start (Seconds (2.0));
    clientApps2.Stop (Seconds (10.0));


    FlowMonitorHelper flowmon ;
    Ptr<FlowMonitor> monitor = flowmon.InstallAll( ) ;



    pointToPoint1.EnablePcapAll("prog1");
    pointToPoint2.EnablePcapAll("prog2");
    Simulator::Stop(Seconds(50.0) ) ;
    Simulator::Run();

    monitor->CheckForLostPackets() ;
    Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier()) ;
    std :: map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ( ) ;

    for (std ::map<FlowId,FlowMonitor::FlowStats >::const_iterator iter = stats.begin();iter!= stats.end( );++iter) //map iterator
    {
        Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow ( iter->first ) ;
        if ((t.sourceAddress==Ipv4Address ("10.1.1.1") && t.destinationAddress==Ipv4Address("10.1.1.2")))
        {
             std:: cout<<"Flow ID : " <<iter->first<<"source address" << t.sourceAddress<<"destination address"<<t.destinationAddress<<"\n" ;
             std :: cout<<"Transmitted Packets ="<<iter->second.txPackets<<"\n" ;
             std :: cout<<"Received Packets = "<<iter->second.rxPackets<<"\n" ;
             std :: cout<<"Throughput is :" <<iter->second.rxBytes*8.0/(iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds())/1024/1024<<"Mbps.\n" ;
        }
    }

    Simulator::Destroy ();
    return 0;
}

0 个答案:

没有答案