title: How to print packets received using UDP in NS-3?
body: <p>I am neither able to pass receiving socket address using parameters, nor able to bind <code>setrecvcallback</code> method with socket. </p>
  
  <p>The code below does not print anything. How can I print those packets?</p>
  
  <pre><code>//included needed header files
  
  using namespace ns3;
  using namespace std;
  
  class MyApp : public Application
  {
  public:
  
    MyApp ();
    virtual ~MyApp();
  
    void Setup (Ptr&lt;Node&gt; node,Address raddress, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
  
  private:
    virtual void StartApplication (void);
    virtual void StopApplication (void);
  
    void ScheduleTx (void);
    void SendPacket (void);
    void ReceivePacket (Ptr&lt;Socket&gt;);
  
    Ptr&lt;Socket&gt;     m_socket;
    Ptr&lt;Socket&gt;     m_rsocket;
    Address         m_peer;
    uint32_t        m_packetSize;
    uint32_t        m_nPackets;
    DataRate        m_dataRate;
    EventId         m_Event;
    bool            m_running;
    uint32_t        m_packetsReceived;
    uint32_t        m_packetsSent;
    Ptr&lt;Packet&gt;     m_spacket;
    Ptr&lt;Packet&gt;     m_rpacket;
    uint8_t*        rxPayload;
    Address         m_raddress;
    Ptr&lt;Node&gt;            node;
  };
  
  MyApp::MyApp ()
    : m_socket (0),
      m_peer (),
      m_packetSize (0),
      m_nPackets (0),
      m_dataRate (0),
      m_Event (),
      m_running (false),
      m_packetsSent (0)
  {
  }
  
  MyApp::~MyApp()
  {
    m_socket = 0;
  }
  
  void
  MyApp::Setup (Ptr&lt;Node&gt; node,Address raddress ,Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
  {
    //m_socket = socket;
    this-&gt;node=node;
    m_peer = address;
    m_packetSize = packetSize;
    m_nPackets = nPackets;
    m_dataRate = dataRate;
    m_raddress=raddress;
    //Socket::CreateSocket (nodes.Get (0), UdpSocketFactory::GetTypeId ())
  }
  
  void
  MyApp::StartApplication (void)
  {
    m_socket = Socket::CreateSocket (node, UdpSocketFactory::GetTypeId ());
    m_running = true;
    m_packetsSent = 0;
    m_packetsReceived=0;
    m_socket-&gt;Bind ();
  
    Ptr&lt;SocketFactory&gt; sockFactory = node-&gt;GetObject&lt;UdpSocketFactory&gt; ();
    m_rsocket=sockFactory-&gt;CreateSocket ();
    m_rsocket-&gt;Bind (InetSocketAddress (Ipv4Address::GetAny (), 8085));
  
    m_rsocket-&gt;Connect (m_peer);
    m_socket-&gt;Connect (m_peer);
      m_socket-&gt;SetRecvCallback (MakeCallback(&amp;MyApp::ReceivePacket,this));
       m_rsocket-&gt;SetRecvCallback (MakeCallback(&amp;MyApp::ReceivePacket,this));
    SendPacket ();
  }
  
  void
  MyApp::StopApplication (void)
  {
    m_running = false;
  
    if (m_Event.IsRunning ())
      {
        Simulator::Cancel (m_Event);
      }
  
    if (m_socket)
      {
        m_socket-&gt;Close ();
      }
  }
  void
  MyApp::SendPacket (void)
  {
    std::stringstream msg;//msg2;
  
    msg &lt;&lt; "My pay load is my payload";
    m_packetSize=msg.str ().length ();
    m_spacket = Create&lt;Packet&gt; ((uint8_t*) msg.str().c_str(), msg.str().length());
  
    uint8_t *buffer = new uint8_t  [m_spacket-&gt;GetSize ()];
    memset(buffer, 0, m_spacket-&gt;GetSize () +1);
    m_spacket-&gt;CopyData (buffer, m_spacket-&gt;GetSize ());
  
    std::cout&lt;&lt;m_packetsReceived&lt;&lt; "\n";
    if(m_packetsReceived&gt;0)std::cout&lt;&lt;"received payload:"&lt;&lt;rxPayload;
    m_socket-&gt;Send (m_spacket);
    if (++m_packetsSent &lt; m_nPackets)
      {
        ScheduleTx ();
      }
  }
  
  void
  MyApp::ReceivePacket (Ptr&lt;Socket&gt; socket)
  {
  
    //uint32_t availableData;
    //availableData = socket-&gt;GetRxAvailable ();
    ++m_packetsReceived;
    m_rpacket = socket-&gt;Recv (std::numeric_limits&lt;uint32_t&gt;::max (), 0);
  
  
      rxPayload = new uint8_t  [m_rpacket-&gt;GetSize ()];
     memset(rxPayload, 0, m_packetSize );
  
     m_rpacket-&gt;CopyData (rxPayload, m_rpacket-&gt;GetSize ());
  }
  
  void
  MyApp::ScheduleTx (void)
  {
    if (m_running)
      {
        Time tNext (Seconds (m_packetSize * 8 / static_cast&lt;double&gt; (m_dataRate.GetBitRate ())));
        m_Event = Simulator::Schedule (tNext, &amp;MyApp::SendPacket, this);
  
      }
  }
  
  
  int
  main (int argc, char *argv[])
  {
    NodeContainer wifiStaNodes;
    wifiStaNodes.Create (1);
    NodeContainer wifiApNode;
    wifiApNode.Create (1);
  
    YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
    YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
    phy.SetChannel (channel.Create ());
  
    WifiHelper wifi;
    wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
  
    WifiMacHelper mac;
    Ssid ssid = Ssid ("ns-3-ssid");
    mac.SetType ("ns3::StaWifiMac",
                 "Ssid", SsidValue (ssid),
                 "ActiveProbing", BooleanValue (false));
  
    NetDeviceContainer staDevices;
    staDevices = wifi.Install (phy, mac, wifiStaNodes);
  
    mac.SetType ("ns3::ApWifiMac",
                 "Ssid", SsidValue (ssid));
  
    NetDeviceContainer apDevices;
    apDevices = wifi.Install (phy, mac, wifiApNode);
  
    NodeContainer nodes;
    nodes.Add (wifiStaNodes);
    nodes.Add (wifiApNode);
  
    NetDeviceContainer devices;
    devices.Add (staDevices);
    devices.Add (apDevices);
  
    MobilityHelper mobility;
  
    mobility.SetMobilityModel ("ns3::SteadyStateRandomWaypointMobilityModel",
                               "MinSpeed",DoubleValue (5.0),"MaxSpeed",DoubleValue (10.0),
                               "MinPause",DoubleValue (0.04),"MaxPause",DoubleValue (0.04),
                               "MinX", DoubleValue (0.0),"MaxX", DoubleValue (20.0),
                               "MinY", DoubleValue (0.0),"MaxY", DoubleValue (20.0));
  
    mobility.Install (nodes);
  
    /*Ptr&lt;RateErrorModel&gt; em = CreateObject&lt;RateErrorModel&gt; ();
    em-&gt;SetAttribute ("ErrorRate", DoubleValue (0.00001));
    devices.Get (1)-&gt;SetAttribute ("ReceiveListErrorModel", PointerValue (em));
  */
  
    InternetStackHelper stack;
    stack.Install (nodes);
  
    Ipv4AddressHelper address;
    address.SetBase ("10.1.1.0", "255.255.255.252");
    Ipv4InterfaceContainer interfaces = address.Assign (devices);
    //address.Assign (apDevices);
  
    Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
  
    //device 0 is sta and device 1 is ap
  
    PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 8080));
    ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (0));
    sinkApps.Start (Seconds (0.));
    sinkApps.Stop (Seconds (20.));
  
    PacketSinkHelper packetSinkHelper2 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 8085));
    ApplicationContainer sinkApps2 = packetSinkHelper2.Install (nodes.Get (1));
    sinkApps2.Start (Seconds (0.));
    sinkApps2.Stop (Seconds (20.));
  
    Ptr&lt;MyApp&gt; app = CreateObject&lt;MyApp&gt; ();
    app-&gt;Setup (nodes.Get(1),InetSocketAddress (interfaces.GetAddress (1), 8085),InetSocketAddress (interfaces.GetAddress (0), 49153), 1040, 5, DataRate ("1Mbps"));
    nodes.Get (1)-&gt;AddApplication (app);
    app-&gt;SetStartTime (Seconds (1.));
    app-&gt;SetStopTime (Seconds (20.));
  
    Ptr&lt;MyApp&gt; app2 = CreateObject&lt;MyApp&gt; ();
    app2-&gt;Setup (nodes.Get(0),InetSocketAddress (interfaces.GetAddress (0),8080), InetSocketAddress (interfaces.GetAddress (1), 49153), 1040, 5, DataRate ("1Mbps"));
    nodes.Get (0)-&gt;AddApplication (app2);
    app2-&gt;SetStartTime (Seconds (1.));
    app2-&gt;SetStopTime (Seconds (20.));
  
    AnimationInterface anim("test.xml");
    anim.EnablePacketMetadata (true);
  
    Simulator::Stop (Seconds (5));
    Simulator::Run ();
    Simulator::Destroy ();
  
    return 0;
  }
  </code></pre>