我在python中编写了一个代码,用于在ns3中进行套接字编程。在我的代码中,我可以看到源将数据包发送到接收器,但是接收器将不响应以接收该数据包(实际上,我相信“ RecPkt”方法未执行,但我不知道为什么)。这是我的代码中与发送和接收数据包的方法有关的部分:
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()
这里是用于定义源和接收器的代码:
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))
这是与每个时间间隔重复发送数据包有关的代码的最后一部分。例如,如果pktcount = 2,则源将发送两个数据包:
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()
这是我得到的结果:
Configure Tracing.
Run Simulation.
Sending one packet!
Sending one packet!
root@far-System:
请您帮助我,我的错误在哪里?
非常感谢
谢谢您的提问!即使没有答案,它也让我希望NS-3套接字可以与Python一起使用,因此让我作了进一步研究。
我无法直接为您解决问题(回答Mateus Sousa关于端口号的评论可能会有所帮助),但是根据您的代码和一些C ++示例,我创建了以下工作示例。我希望您可以从中受益并找到实现中的错误。
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()