NS3 socket programming

69 Views Asked by At

#include "ns3/applications-module.h"
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/socket.h"

#include <fstream>

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("UdpClientServerExample");

void ReceivePacket(Ptr<Socket> socket)
{
    Ptr<Packet> packet;
    Address from;
    while ((packet = socket->RecvFrom(from)))
    {
        NS_LOG_UNCOND("Received " << packet->GetSize() << " bytes from " << from);
    }
}

int main(int argc, char *argv[])
{
    // Declare variables used in command-line arguments
    bool logging = true;
    uint16_t serverPort = 7777; // Use uint16_t for the port
    Address serverAddress(InetSocketAddress(Ipv4Address("172.26.34.69"), serverPort)); // Server address

    CommandLine cmd(__FILE__);
    cmd.AddValue("logging", "Enable logging", logging);
    cmd.Parse(argc, argv);

    if (logging)
    {
        LogComponentEnable("UdpClient", LOG_LEVEL_INFO);
        LogComponentEnable("UdpServer", LOG_LEVEL_INFO);
    }

    NS_LOG_INFO("Create nodes in above topology.");
    NodeContainer n;
    n.Create(2);

    InternetStackHelper internet;
    internet.Install(n);

    NS_LOG_INFO("Create channel between the two nodes.");
    CsmaHelper csma;
    csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
    csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
    csma.SetDeviceAttribute("Mtu", UintegerValue(1400));
    NetDeviceContainer d = csma.Install(n);

    NS_LOG_INFO("Assign IP Addresses.");
    Ipv4AddressHelper ipv4;
    ipv4.SetBase("10.1.1.0", "255.255.255.0");
    Ipv4InterfaceContainer i = ipv4.Assign(d);

    NS_LOG_INFO("Create UdpServer application on node 1.");
    UdpServerHelper server(serverPort);
    ApplicationContainer serverApps = server.Install(n.Get(1));
    serverApps.Start(Seconds(1.0));
    serverApps.Stop(Seconds(10.0));

    NS_LOG_INFO("Create UdpClient application on node 0 to send to external server.");
    // Update client address to "192.168.175.128"
    Ipv4Address clientAddress("192.168.175.128");
    InetSocketAddress clientSocketAddress(clientAddress, serverPort);
    UdpClientHelper client(clientSocketAddress); // Updated to use external server's IP and port
    client.SetAttribute("MaxPackets", UintegerValue(0xFFFFFFFF));
    client.SetAttribute("Interval", TimeValue(Seconds(1.0)));
    client.SetAttribute("PacketSize", UintegerValue(1024));
    ApplicationContainer clientApps = client.Install(n.Get(0));
    clientApps.Start(Seconds(2.0));
    clientApps.Stop(Seconds(10.0));

    // Create a UDP socket on node 0 to receive external data
    TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
    Ptr<Socket> recvSocket = Socket::CreateSocket(n.Get(0), tid);
    InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), serverPort);
    recvSocket->Bind(local);
    recvSocket->SetRecvCallback(MakeCallback(&ReceivePacket));

    NS_LOG_INFO("Run Simulation.");
    Simulator::Run();
    Simulator::Destroy();
    NS_LOG_INFO("Done.");

    return 0;
}

i am working on a project where ns3 communicate with external applications and recieve data through sockets for that i am writing code using socketfactory application in ns3.my code is building successfully and executing without any error but i am not sure whether the communication is establishing or not .if anyone done this type of work before please guide me.

i am writing code using socketfactory application in ns3.my code is building successfully and executing without any error but i am not sure whether the communication is establishing or not i want to communicate both application using sockets.

0

There are 0 best solutions below