[英]Socket programming in ns3 with python
我用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.