在ns3中模拟连续的客户端 - 服务器通信

时间:2017-09-11 04:53:58

标签: sockets networking simulation ns-3

我想模拟一个简单的连续客户端 - 服务器请求 - 响应行为;即客户端向服务器发送数据包,然后服务器接收数据包并响应客户端,然后客户端接收响应数据包,然后再向服务器发送新数据包,依此类推。我已经想出发送一轮通信(客户端 - >服务器 - >客户端),但不知道如何继续这样做。这是我实现一轮的代码:

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (wifiApNode.Get (nWifiAp - 1));
serverApps.Start (Seconds (1.0));
serverApps.Stop (Seconds (10.0));

UdpEchoClientHelper echoClient (apDevicesInterfaces.GetAddress (nWifiAp - 1), 9);
echoClient.SetAttribute ("MaxPackets", UintegerValue (1));
echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));
echoClient.SetAttribute ("PacketSize", UintegerValue (1024));

ApplicationContainer clientApps;

clientApps = echoClient.Install (wifiStaNodes.Get (nWifiSta - 1));
clientApps.Start (Seconds (2.0));
clientApps.Stop (Seconds (10.0));

如果我在echoClient.SetAttribute ("MaxPackets", UintegerValue (1));中设置了除1之外的任何其他int,我可以进行那么多轮,但问题是它们都以1秒的间隙开始(由于这个原因:echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0)));)。我希望客户端在收到服务器响应后立即开始发送,而不是在等待1秒后发送。

1 个答案:

答案 0 :(得分:0)

您需要修改echoClient应用程序,特别是可以接受数据包的“HandleRead”方法。 目前它只打印收到一个。看一下UdpEchoServer应用程序,HandleRead在该应用程序中生成响应。