简体   繁体   English

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

[英]Socket programming in ns3 with python

I wrote a code in python for doing socket programming in ns3.我用python编写了一段代码,用于在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).在我的代码中,我可以看到源向接收器发送了一个数据包,但接收器不会响应接收数据包(实际上我相信“RecPkt”方法没有执行,但我不知道为什么)。 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:例如,如果 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()

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.即使没有答案,它也让我希望 NS-3 套接字可以与 Python 一起使用,因此让我进一步调查。

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.我无法直接帮助您解决您的问题(回答 Mateus Sousa 关于端口号的评论可能会有所帮助),但根据您的代码和一些 C++ 示例,我创建了以下工作示例。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM