I've tried to retry from the groud up multiple times but always end up at the same error when using "socket.Send". Do i have to make another cppyy.cppdef function to make sure it sends properly?
ns.cppyy.cppdef("""
namespace ns3
{
void RecvCallback(Ptr<Socket> socket) {
if (socket->Recv()) {
std::cout << "Received one packet!" << std::endl;
}
}
Callback<void, Ptr<Socket>> MakeRecvCallback()
{
return MakeCallback(&RecvCallback);
}
}
""")
ns.LogComponentEnable("UdpSocketImpl", ns.LOG_LEVEL_INFO)
ns.LogComponentEnable("Ipv4L3Protocol", ns.LOG_LEVEL_INFO)
ns.LogComponentEnable("ArpL3Protocol", ns.LOG_LEVEL_INFO)
packetsize = 64 #bytes
pktcount = 2
pktinterval = 0.25 #seconds
nodes = ns.NodeContainer()
nodes.Create(2)
p2p = ns.PointToPointHelper()
p2p.SetDeviceAttribute("DataRate", ns.StringValue("5Mbps"))
p2p.SetChannelAttribute("Delay", ns.StringValue("2ms"))
devices = p2p.Install(nodes)
internet = ns.InternetStackHelper()
internet.Install(nodes)
address = ns.Ipv4AddressHelper()
address.SetBase(ns.Ipv4Address("10.1.1.0"), ns.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign(devices)
def RecPkt(socket):
if socket.Recv():
print ("Received one packet!")
def SndPkt(socket):
if pktcount > 0:
socket.Send(ns.Packet(1024))
#ns.Simulator.Schedule(pktinterval, SndPkt, socket, packetsize, pktcount-1, pktinterval)
print ("Sending one packet!")
else:
socket.Close()
appSource = nodes.Get(0)
appSink = nodes.Get(1)
remoteAddr = interfaces.GetAddress(1)
sink = ns.Socket.CreateSocket(appSink, ns.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 80).ConvertTo())
# Use the C++ MakeRecvCallback function
MakeRecvCallback = ns.MakeRecvCallback
recv_callback = MakeRecvCallback()
sink.SetRecvCallback(recv_callback)
source = ns.Socket.CreateSocket(appSource, ns.TypeId.LookupByName("ns3::UdpSocketFactory"))
source.Connect(ns.InetSocketAddress(remoteAddr, 80).ConvertTo())
ns.Simulator.Schedule(ns.Seconds(30.0), SndPkt(source))
#source.Send(ns.Packet(1))
print ("Run Simulation.")
ns.Simulator.Stop(ns.Seconds(100))
ns.Simulator.Run()
ns.Simulator.Destroy()