我正在尝试构建具有以下特征的 ns-3 模拟。我想要多个客户端节点通过 WiFi 网络使用 UDP 数据包定期与服务器通信(仅与服务器通信,没有客户端到客户端通信)。服务器将接收数据包并回复。我最终想收集每个客户端-服务器连接的延迟。
我对 ns-3 很陌生,我正在尝试构建一个网络,客户端将数据包发送到服务器,但似乎尽管我所有的客户端都发送数据包,但在不同的时刻,服务器只“监听”一个节点。这是我的代码和输出:
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/applications-module.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/packet.h"
#include "ns3/log.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("WirelessNetwork");
void CourseChange(std::string context, Ptr\<const MobilityModel\> model) {
Vector position = model-\>GetPosition();
NS_LOG_UNCOND(context \<\<" x = " \<\< position.x \<\< ", y = " \<\< position.y);
}
// void PacketSentCallback(Ptr\<const Packet\> packet)
// {
// NS_LOG_UNCOND("Packet sent at " \<\< Simulator::Now().GetSeconds() \<\< " seconds");
// }
// void PacketReceivedCallback(Ptr\<const Packet\> packet)
// {
// NS_LOG_UNCOND("Packet received at " \<\< Simulator::Now().GetSeconds() \<\< " seconds");
// }
int main (int argc, char \*argv\[\])
{
// Enable logging for this script
LogComponentEnable ("WirelessNetwork", LOG_LEVEL_INFO);
bool verbose = true;
uint32_t nWifi=3;
double simulationTime = 10; // simulation time in seconds
CommandLine cmd;
cmd.AddValue("nWifi", "Number of wifi STA devices", nWifi);
cmd.AddValue("verbose", "Tell echo applications to log if true", verbose);
cmd.Parse(argc,argv);
if (verbose) {
LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
LogComponentEnable("UdpSocketImpl", LOG_LEVEL_INFO);
LogComponentEnable("UdpClient", LOG_LEVEL_INFO);
LogComponentEnable("UdpServer", LOG_LEVEL_INFO);
}
// Create nodes
NodeContainer wifiStaNode;
wifiStaNode.Create(1);
NodeContainer wifiApNode;
wifiApNode.Create(nWifi);
// Create wireless channel
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy;
phy.SetChannel(channel.Create());
// Default IEEE 802.11n (2.4 GHz)
WifiHelper wifi;
wifi.SetStandard(WIFI_STANDARD_80211n);
// wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
// "DataMode",
// StringValue("HtMcs7"),
// "ControlMode",
// StringValue("HtMcs0"));
WifiMacHelper mac;
// Install PHY and MAC
Ssid ssid = Ssid("ns3-wifi");
mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
NetDeviceContainer staDevice;
staDevice = wifi.Install(phy, mac, wifiStaNode);
mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
NetDeviceContainer apDevice;
apDevice = wifi.Install(phy, mac, wifiApNode);
// Mobility
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0));
positionAlloc->Add(Vector(1.0, 0.0, 0.0));
positionAlloc->Add(Vector(2.0, 0.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(wifiApNode);
mobility.Install(wifiStaNode);
// Internet stack
InternetStackHelper stack;
stack.Install(wifiApNode);
stack.Install(wifiStaNode);
Ipv4AddressHelper address;
address.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer staNodeInterface;
Ipv4InterfaceContainer apNodeInterface;
staNodeInterface = address.Assign(staDevice);
apNodeInterface = address.Assign(apDevice);
// Setting applications
uint16_t port = 9;
PacketSinkHelper packetSinkHelper("ns3::UdpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), port));
ApplicationContainer serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
serverApp.Start(Seconds(0.0));
serverApp.Stop(Seconds(simulationTime + 1));
ApplicationContainer clientApps;
for (uint32_t i = 0; i < wifiApNode.GetN(); ++i)
{
UdpClientHelper client(staNodeInterface.GetAddress(0), port); // Target the server IP
client.SetAttribute("MaxPackets", UintegerValue(2));
client.SetAttribute("PacketSize", UintegerValue(1472));
ApplicationContainer clientApp = client.Install(wifiApNode.Get(i));
clientApp.Start(Seconds(1.0+i*2));
clientApp.Stop(Seconds(10 + 1));
clientApps.Add(clientApp);
}
// Populate routing table
Ipv4GlobalRoutingHelper::PopulateRoutingTables();
// Set simulation time and launch simulation
Simulator::Stop(Seconds(simulationTime + 1));
Simulator::Run();
Simulator::Destroy();
return 0;
}
TraceDelay TX 1472 bytes to 192.168.1.1 Uid: 36 Time: +1sAt time +1.00514s packet sink received 1472 bytes from 192.168.1.2 port 49153 total Rx 1472 bytesTraceDelay TX 1472 bytes to 192.168.1.1 Uid: 71 Time: +2sAt time +2.00024s packet sink received 1472 bytes from 192.168.1.2 port 49153 total Rx 2944 bytesTraceDelay TX 1472 bytes to 192.168.1.1 Uid: 102 Time: +3sTraceDelay TX 1472 bytes to 192.168.1.1 Uid: 133 Time: +4sTraceDelay TX 1472 bytes to 192.168.1.1 Uid: 165 Time: +5sTraceDelay TX 1472 bytes to 192.168.1.1 Uid: 199 Time: +6s
我不知道它是否正确,但它似乎有效,我用 UdpServer 做到了
NS_LOG_INFO("Create UdpServer application on AP node.");
uint16_t port = 4000;
UdpServerHelper server(port);
ApplicationContainer apps = server.Install(wifiApNode.Get(0));
apps.Start(Seconds(1.0));
apps.Stop(Seconds(10.0));
NS_LOG_INFO("Create UdpClient application on node 0 to send to node 1.");
uint32_t MaxPacketSize = 1024;
Time interPacketInterval = Seconds(1);
uint32_t maxPacketCount = 3;
ApplicationContainer clientApps;
UdpClientHelper client(serverAddress, port);
client.SetAttribute("MaxPackets", UintegerValue(maxPacketCount));
client.SetAttribute("Interval", TimeValue(interPacketInterval));
client.SetAttribute("PacketSize", UintegerValue(MaxPacketSize));
clientApps= client.Install(wifiStaNodes);
clientApps.Start(Seconds (2.0));
clientApps.Stop(Seconds (10.0));