I wrote a code in python for doing socket programming in ns3. In my code I can see the source sends a packet to the sink, but sink will not respond to receive the packet (Actually I believe "RecPkt" method is not executing, but I don't know why). Here is the part of my code related to methods for sending and receiving packets:
packetsize = 64 #bytes
pktcount = 2
pktinterval = 0.25 #seconds
def RecPkt(socket):
while socket.Recv():
print "Received one packet!"
def SndPkt(socket, packetsize, pktcount, pktinterval):
if pktcount > 0:
socket.Send(ns.network.Packet(packetsize))
ns.core.Simulator.Schedule(pktinterval, SndPkt, socket, packetsize, pktcount-1, pktinterval)
print "Sending one packet!"
else:
socket.Close()
Here is the code for defining the source and the sink:
appSource = ns.network.NodeList.GetNode(1)
appSink = ns.network.NodeList.GetNode(20)
remoteAddr = appSink.GetObject(ns.internet.Ipv4.GetTypeId()).GetAddress(1,0).GetLocal()
sink = ns.network.Socket.CreateSocket(appSink, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80))
sink.SetRecvCallback(RecPkt)
source = ns.network.Socket.CreateSocket(appSource, ns.core.TypeId.LookupByName("ns3::UdpSocketFactory"))
source.Connect(ns.network.InetSocketAddress(remoteAddr, port))
here is the last part of the code relating to repeating sending the packet for each interval. For example if the pktcount=2 then the source will send two packets:
ns.core.Simulator.Schedule(ns.core.Seconds(30.0), SndPkt, source, packetsize, pktcount, pktinterval)
print "Run Simulation."
ns.core.Simulator.Stop(ns.core.Seconds(stopTime))
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
Here is the result that I am getting:
Configure Tracing.
Run Simulation.
Sending one packet!
Sending one packet!
root@far-System:
Would you please help me where is my mistake?
Thanks a lot
Thanks for the question! Even though there is no answer it gave me hope that NS-3 sockets might work with Python and therefore made me investigate further.
I cannot directly help you with your problem (answer to the comment by Mateus Sousa about port number might be helpful), but based on your code and some C++ examples I have created the following working example. I hope you can benefit from it and find the bug in your implementation.
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
nodes = ns.network.NodeContainer()
nodes.Create(2)
pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2000ms"))
devices = pointToPoint.Install(nodes)
stack = ns.internet.InternetStackHelper()
stack.Install(nodes)
address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"),
ns.network.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign(devices)
source = ns.network.Socket.CreateSocket(
nodes.Get(0),
ns.core.TypeId.LookupByName("ns3::UdpSocketFactory")
)
sink = ns.network.Socket.CreateSocket(
nodes.Get(1),
ns.core.TypeId.LookupByName("ns3::UdpSocketFactory")
)
def send_packet(socket):
print("sending", ns.core.Simulator.Now())
socket.Send(ns.network.Packet(5))
def rcv_packet(socket):
print("received", ns.core.Simulator.Now())
sink.SetRecvCallback(rcv_packet)
sink_address = ns.network.InetSocketAddress(interfaces.GetAddress(1), 4477)
any_address = ns.network.InetSocketAddress(
ns.network.Ipv4Address.GetAny(), 4477
)
sink.Bind(any_address)
source.Connect(sink_address)
ns.core.Simulator.Schedule(
ns.core.Seconds(0.0), send_packet, source,
)
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.