gpt4 book ai didi

python - 使用Python在ns3中进行套接字编程

转载 作者:行者123 更新时间:2023-12-03 11:55:21 27 4
gpt4 key购买 nike

我在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()

关于python - 使用Python在ns3中进行套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36468422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com