使用NS3协议通过UdpL4Protocol发送报文
Send a packet through UdpL4Protocol with NS3
我试图在ns3(c++)中运行模拟,其中节点使用"Send"方法向另一个节点发送数据包(包含在ns3库中)。程序构建正确,但我得到一个分割错误,当我运行它。我检查了参数,我没有对指针做任何花哨的操作
似乎错误来自方法"发送",但我无法弄清楚到底是什么问题,我尝试显示IP地址,他们似乎是对的,我试图创建包与默认构造函数,参数。我也尝试使用IPv6地址,但它也不工作。
请帮帮我,我无计可施
下面是我的代码:#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/wifi-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/mobility-module.h"
#include <vector>
#include <string.h>
using namespace ns3;
void velocityHandler(Ptr<ConstantVelocityMobilityModel> vel, Vector3D vec) {
vel->SetVelocity(vec);
}
void sendHandler(UdpL4Protocol* udp,NodeContainer nodes, Ptr<Packet> paquet) {
Ptr<Ipv4> ipv4 = nodes.Get(1)->GetObject<Ipv4>();
Ptr<Ipv4> ipv42 = nodes.Get(0)->GetObject<Ipv4>();
Ipv4Address adr = ipv4->GetAddress(1,0).GetLocal();
Ipv4Address adr2 = ipv42->GetAddress(1,0).GetLocal();
udp->Send(paquet, adr,adr2,(uint16_t)50 ,(uint16_t)60/*,Ptr<Ipv4Route>(route)*/);
}
int main() {
Time::SetResolution (Time::MS);
//Creation des Noeuds
NodeContainer nodes1, nodes2;
nodes1.Create(1);
nodes2.Create(2);
//Positionnement des noeuds
MobilityHelper mobility1, mobility2;
mobility1.SetPositionAllocator("ns3::GridPositionAllocator","MinX",
DoubleValue(0.),"MinY",DoubleValue(6.));
mobility2.SetPositionAllocator("ns3::GridPositionAllocator","MinX",
DoubleValue(2.),"MinY",DoubleValue(0.),
"DeltaX",DoubleValue(2.),"DeltaY",
DoubleValue(4.),"GridWidth",UintegerValue(1),
"LayoutType",StringValue("RowFirst"));
//Deplacements des noeuds
mobility1.SetMobilityModel("ns3::ConstantVelocityMobilityModel");
mobility1.Install(nodes1);
Ptr<ConstantVelocityMobilityModel> velmodel = nodes1.Get(0)->GetObject<ConstantVelocityMobilityModel>();
(*velmodel).SetVelocity(Vector3D(0.,0.,0.));
mobility2.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility2.Install(nodes2);
//liens
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue ("2Mbps"));
p2p.SetChannelAttribute("Delay", StringValue ("2ms"));
//Interfaces
NetDeviceContainer devices;
devices = p2p.Install(nodes2);
//Pile protocolaire
InternetStackHelper stack;
stack.Install(nodes2);
//Adressage
Ipv4AddressHelper address;
address.SetBase("30.0.0.0","255.255.255.0");
Ipv4InterfaceContainer interfaces = address.Assign(devices);
//UDP
UdpL4Protocol* udp = new UdpL4Protocol();
udp->SetNode(nodes2.Get(0));
udp->SetNode(nodes2.Get(1));
//lancement de la simulation
Packet a = Packet();
AnimationInterface anim ("animudp.xml");
Simulator::Schedule(Seconds(1.),&velocityHandler, velmodel, Vector3D(5.,0.,0.));
Simulator::Schedule(Seconds(0.2),&sendHandler,udp, nodes2, Ptr<Packet>(&a));
Simulator::Run();
Simulator::Stop(Seconds(10.));
}
和我从调试器得到的消息:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff5919900 in ns3::Callback<void, ns3::Ptr<ns3::Packet>, ns3::Ipv4Address, ns3::Ipv4Address, unsigned char, ns3::Ptr<ns3::Ipv4Route>, ns3::empty, ns3::empty, ns3::empty, ns3::empty>::operator() (this=0x66aa40, a1=..., a2=..., a3=..., a4=17 ' 21', a5=...)
at ./ns3/callback.h:1077
1077 return (*(DoPeekImpl ()))(a1,a2,a3,a4,a5);
您是否遵循了下面的链接程序?http://www.nsnam.org/wiki/HOWTO_understand_and_find_cause_of_terminated_with_signal_errors