L.Emil
L.Emil

Reputation: 161

Ns3 LTE simulation: UDP-server-client setup, issues with sending and receiving

"I have the following code. I want to send packets from one UDP client socket belonging to Node 1 to three other server sockets belonging to Node 1, 2, and 3 (one of the server sockets belongs to the same node as the sending socket). I have a callback function that should execute on receive. The issue I'm facing is that the callback function is only called on receive for the socket that is on the same IP as the sending socket. When sending, it looks like the packet is sent but it is not received. Why isn't the callback function called for the other sockets? All advice is appreciated. The code is as follows, and prints are provided. Thank you in advance."

NodeContainer ueNodes;
ueNodes.Create(3);

NodeContainer enbNodes;
enbNodes.Create(1);



MobilityHelper mobility;
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
// Set positions for the nodes to achieve a fixed distance
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
positionAlloc->Add(Vector(0.0, 0.0, 0.0)); // Position for the first node (0,0,0)
positionAlloc->Add(Vector(1.0, 0.0, 0.0)); // Position for the second node (1,0,0)
positionAlloc->Add(Vector(2.0, 0.0, 0.0)); // Position for the second node (2,0,0)
mobility.SetPositionAllocator(positionAlloc);
mobility.Install(ueNodes);
mobility.Install(enbNodes);



// Create the LTE channel
Ptr<LteHelper> lteHelper = CreateObject<LteHelper>();

// Install LTE devices
NetDeviceContainer enbDevs;
NetDeviceContainer ueDevs;
enbDevs = lteHelper->InstallEnbDevice(enbNodes);
ueDevs = lteHelper->InstallUeDevice(ueNodes);
lteHelper->SetEnbDeviceAttribute("DlBandwidth", UintegerValue(25));
lteHelper->SetEnbDeviceAttribute("UlBandwidth", UintegerValue(25));

// attach must attach for some reason.
lteHelper->Attach(ueDevs, enbDevs.Get(0));
// Activate a data radio bearer
EpsBearer::Qci q = EpsBearer::GBR_CONV_VOICE;
EpsBearer bearer(q);
lteHelper->ActivateDataRadioBearer(ueDevs, bearer);
// lteHelper->EnableTraces();

// Install internet stack on nodes
InternetStackHelper internet;
internet.Install(ueNodes);
internet.Install(enbNodes);

// Assign IP addresses to devices
Ipv4AddressHelper ipv4;
ipv4.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer ueIpIfaces;
Ipv4InterfaceContainer enbIpIfaces;
ueIpIfaces = ipv4.Assign(ueDevs);
enbIpIfaces = ipv4.Assign(enbDevs);

// Create application container for client and server app
ApplicationContainer clientApps, serverApps;
std::vector<InetSocketAddress> bindAdresses;
std::vector<Address> remoteAdresses;
uint16_t udpServerPort = 9;



//ip address for node 1
Ptr<Ipv4> ip1 = ueNodes.Get(0)->GetObject<Ipv4>();
Ipv4Address IpAddress1 = ip1->GetAddress(1, 0).GetLocal();
std::cout << "Address1 " << IpAddress1 << std::endl;

//ip address for node 2
Ptr<Ipv4> ip2 = ueNodes.Get(1)->GetObject<Ipv4>();
Ipv4Address IpAddress2 = ip2->GetAddress(1, 0).GetLocal();
std::cout << "Address2 " << IpAddress2 << std::endl;

//ip address for node 3
Ptr<Ipv4> ip3 = ueNodes.Get(2)->GetObject<Ipv4>();
Ipv4Address IpAddress3 = ip3->GetAddress(1, 0).GetLocal();
std::cout << "Address3 " << IpAddress3 << std::endl;
IpAddress3 = ip3->GetAddress(0, 0).GetLocal();


//create socket for node 1,2,3
Ptr<Socket> serverSocket1 = Socket::CreateSocket(ueNodes.Get(0), UdpSocketFactory::GetTypeId());
std::cout << "p1" << std::endl;
Ptr<Socket> serverSocket2 = Socket::CreateSocket(ueNodes.Get(1), UdpSocketFactory::GetTypeId());
std::cout << "p2" << std::endl;
Ptr<Socket> serverSocket3 = Socket::CreateSocket(ueNodes.Get(2), UdpSocketFactory::GetTypeId());
std::cout << "p3" << std::endl;
InetSocketAddress bindAdress1 = InetSocketAddress(Ipv4Address("192.168.1.1"), udpServerPort); //address for server socket to bind to
std::cout << "p4" << std::endl;
InetSocketAddress bindAdress2 = InetSocketAddress(Ipv4Address("192.168.1.2"), udpServerPort); //address for server socket to bind to
std::cout << "p5" << std::endl;
InetSocketAddress bindAdress3 = InetSocketAddress(Ipv4Address("192.168.1.3"), udpServerPort); //address for server socket to bind to
std::cout << "p6" << std::endl;

//Callback function to be called when packet is received
serverSocket1 -> SetRecvCallback(MakeCallback(ReceiveCallback));
std::cout << "p7" << std::endl;
serverSocket2->SetRecvCallback(MakeCallback(ReceiveCallback));
std::cout << "p8" << std::endl;
serverSocket3->SetRecvCallback(MakeCallback(ReceiveCallback));
std::cout << "p9" << std::endl;

//Bind the server sockets to each address
//if bounded returns 0
int bounded;
bounded = serverSocket1->Bind(bindAdress1);
std::cout << "bound to: " << InetSocketAddress::ConvertFrom(bindAdress1).GetIpv4()
          << "with status: " << bounded << std::endl;
bounded = serverSocket2->Bind(bindAdress2);
std::cout << "bound to: " << InetSocketAddress::ConvertFrom(bindAdress2).GetIpv4()
          << "with status: " << bounded << std::endl;
bounded = serverSocket3->Bind(bindAdress3);
std::cout << "bound to: " << InetSocketAddress::ConvertFrom(bindAdress3).GetIpv4()
          << "with status: " << bounded << std::endl;

//create client sockets only want client sockets for node 1
Ptr<Socket> clientSocket1 = Socket::CreateSocket(ueNodes.Get(0), UdpSocketFactory::GetTypeId());



//sends data from each client socket on node 1 to each serverver socket on node 1,2,3 (one server socket per node)
//if packet is sent returns the size of the packet size of str "Hello, NS-3!"
int sent;
std::string data = "Hello, NS-3!";
Ptr<Packet> packet = Create<Packet>((uint8_t*)data.c_str(), data.size());
sent = clientSocket1->SendTo(packet,0,bindAdress1);                                                       //send to server socket on node 1 ip
std::cout << "sent data: " << data << "with status: " << sent
          << "to: " << InetSocketAddress::ConvertFrom(bindAdress1).GetIpv4() << std::endl;
sent = clientSocket1->SendTo(packet,0,bindAdress2);                                                        //send to server socket on node 2 ip
std::cout << "sent data: " << data << "with status: " << sent
          << "to: " << InetSocketAddress::ConvertFrom(bindAdress2).GetIpv4() << std::endl;
sent = clientSocket1->SendTo(packet,0,bindAdress3);                                                       //send to server socket on node 3 ip
std::cout << "sent data: " << data << "with status: " << sent
          << "to: " << InetSocketAddress::ConvertFrom(bindAdress3).GetIpv4() << std::endl;
// Start and run the simulation
Simulator::Stop(Seconds(20.0));
Simulator::Run();
Simulator::Destroy();




return 0;

Prints:

Upvotes: 0

Views: 183

Answers (0)

Related Questions