c++ - 如何在 NS-3 中打印使用 UDP 接收的数据包?

标签 c++ sockets udp packet ns-3

我既不能使用参数传递接收套接字地址,也不能绑定(bind)setrecvcallback使用套接字的方法。

下面的代码不打印任何内容。我怎样才能打印这些数据包?

//included needed header files

using namespace ns3;
using namespace std;

class MyApp : public Application
{
public:

  MyApp ();
  virtual ~MyApp();

  void Setup (Ptr<Node> 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<Socket>);

  Ptr<Socket>     m_socket;
  Ptr<Socket>     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<Packet>     m_spacket;
  Ptr<Packet>     m_rpacket;
  uint8_t*        rxPayload;
  Address         m_raddress;
  Ptr<Node>            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<Node> node,Address raddress ,Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
{
  //m_socket = socket;
  this->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->Bind ();

  Ptr<SocketFactory> sockFactory = node->GetObject<UdpSocketFactory> ();
  m_rsocket=sockFactory->CreateSocket ();
  m_rsocket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 8085));

  m_rsocket->Connect (m_peer);
  m_socket->Connect (m_peer);
    m_socket->SetRecvCallback (MakeCallback(&MyApp::ReceivePacket,this));
     m_rsocket->SetRecvCallback (MakeCallback(&MyApp::ReceivePacket,this));
  SendPacket ();
}

void
MyApp::StopApplication (void)
{
  m_running = false;

  if (m_Event.IsRunning ())
    {
      Simulator::Cancel (m_Event);
    }

  if (m_socket)
    {
      m_socket->Close ();
    }
}
void
MyApp::SendPacket (void)
{
  std::stringstream msg;//msg2;

  msg << "My pay load is my payload";
  m_packetSize=msg.str ().length ();
  m_spacket = Create<Packet> ((uint8_t*) msg.str().c_str(), msg.str().length());

  uint8_t *buffer = new uint8_t  [m_spacket->GetSize ()];
  memset(buffer, 0, m_spacket->GetSize () +1);
  m_spacket->CopyData (buffer, m_spacket->GetSize ());

  std::cout<<m_packetsReceived<< "\n";
  if(m_packetsReceived>0)std::cout<<"received payload:"<<rxPayload;
  m_socket->Send (m_spacket);
  if (++m_packetsSent < m_nPackets)
    {
      ScheduleTx ();
    }
}

void
MyApp::ReceivePacket (Ptr<Socket> socket)
{

  //uint32_t availableData;
  //availableData = socket->GetRxAvailable ();
  ++m_packetsReceived;
  m_rpacket = socket->Recv (std::numeric_limits<uint32_t>::max (), 0);


    rxPayload = new uint8_t  [m_rpacket->GetSize ()];
   memset(rxPayload, 0, m_packetSize );

   m_rpacket->CopyData (rxPayload, m_rpacket->GetSize ());
}

void
MyApp::ScheduleTx (void)
{
  if (m_running)
    {
      Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
      m_Event = Simulator::Schedule (tNext, &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<RateErrorModel> em = CreateObject<RateErrorModel> ();
  em->SetAttribute ("ErrorRate", DoubleValue (0.00001));
  devices.Get (1)->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<MyApp> app = CreateObject<MyApp> ();
  app->Setup (nodes.Get(1),InetSocketAddress (interfaces.GetAddress (1), 8085),InetSocketAddress (interfaces.GetAddress (0), 49153), 1040, 5, DataRate ("1Mbps"));
  nodes.Get (1)->AddApplication (app);
  app->SetStartTime (Seconds (1.));
  app->SetStopTime (Seconds (20.));

  Ptr<MyApp> app2 = CreateObject<MyApp> ();
  app2->Setup (nodes.Get(0),InetSocketAddress (interfaces.GetAddress (0),8080), InetSocketAddress (interfaces.GetAddress (1), 49153), 1040, 5, DataRate ("1Mbps"));
  nodes.Get (0)->AddApplication (app2);
  app2->SetStartTime (Seconds (1.));
  app2->SetStopTime (Seconds (20.));

  AnimationInterface anim("test.xml");
  anim.EnablePacketMetadata (true);

  Simulator::Stop (Seconds (5));
  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
}

最佳答案

您可以编写以下代码来创建按摩并将其发送到 MyApp::SendPacket (void) 函数:

std::ostringstream msg; msg << "Hello!" << '\0';
Ptr<Packet> packet = Create<Packet> ((uint8_t*) msg.str().c_str(), msg.str().length());  
m_socket->Send (packet);

然后在此函数中编写以下代码以打印您的有效负载:
uint8_t *buffer = new uint8_t[packet->GetSize ()];
packet->CopyData(buffer, packet->GetSize ());
std::string s = std::string((char*)buffer);
std::cout << s << std::endl; 

关于c++ - 如何在 NS-3 中打印使用 UDP 接收的数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629419/

相关文章:

c++ - 在模板推导时,是否可以检测是否可以运行 consteval 函数

c++ - 安全使用 strcpy

java - 在JAVA中提高Netty Server的性能

java - Servlet可以打开套接字与本地代码对话吗?

c - 如何确保 UDP 套接字已关闭?

从其他方法返回 vector 时,C++ 无法访问 vector 元素

sockets - HaProxy(无法绑定(bind)套接字,选择测试失败)

javascript - 如何从 Javascript/浏览器发送 M-SEARCH

c++ - 如何在 C++ 中使用 winsock 接收 udp 数据包?

c++ - 在Visual C++中开发静态库以进行有效的数值计算