NS-3 python

102 views
Skip to first unread message

tamuct ccilab

unread,
Oct 13, 2023, 2:46:34 PM10/13/23
to ns-3-users
I am trying to convert the manet-routing-compare.cc to python.
For setup SetupPacketReceive function, I convert it as:
def SetupPacketReceive(addr, node):
tid = ns.core.TypeId.LookupByName("ns3::UdpSocketFactory")
sink = ns.network.Socket.CreateSocket(node, tid)
local = ns.network.InetSocketAddress(ns.network.Ipv4Address(addr), port.value)
print(local)
sink.Bind(local)
sink.SetRecvCallback(ns.core.MakeCallback(ReceivePacket(sink)))

return sink

I am getting the following error:
sink.Bind(local)
TypeError: none of the 2 overloaded methods succeeded. Full details:
  int ns3::Socket::Bind(const ns3::Address& address) =>
    TypeError: could not convert argument 1
  int ns3::Socket::Bind() =>
    TypeError: takes at most 0 arguments (1 given)

The Bind function in Socket can not take argument.
How can i bind a local address to a socket?

Tommaso Pecorella

unread,
Oct 13, 2023, 7:04:37 PM10/13/23
to ns-3-users
I'm not 100% sure, but try to use: "sink.Bind(local.ConvertTo())".

The reason is that Python doesn't do type conversions like C++ does, so you have to convert an InetSocketAddress to an Address.

tamuct ccilab

unread,
Oct 16, 2023, 12:39:54 PM10/16/23
to ns-3-users
sink.Bind(local.ConvertTo()) command does not work here. It shows following error:

sink.Bind(local.ConvertTo())
AttributeError: 'InetSocketAddress' object has no attribute 'ConvertTo'. Did you mean: 'ConvertFrom'?

tamuct ccilab

unread,
Oct 16, 2023, 1:26:20 PM10/16/23
to ns-3-users
This is the solution I get from : https://www.nsnam.org/docs/manual/html/python.html

# Explicitly convert the InetSocketAddress to Address using InetSocketAddress.ConvertTo()
sink.Bind(ns.network.InetSocketAddress(ns.network.Ipv4Address.GetAny(), 80).ConvertTo())

But this still shows the same error: AttributeError: 'InetSocketAddress' object has no attribute 'ConvertTo'. Did you mean: 'ConvertFrom'?

Tommaso Pecorella

unread,
Oct 16, 2023, 3:01:45 PM10/16/23
to ns-3-users
No, ConvertFrom is to do the opposite. ConvertTo should work. I'll forward the question to our python wizards.

Gabriel Ferreira

unread,
Oct 16, 2023, 3:11:10 PM10/16/23
to ns-3-users
If you don't have ConvertTo(), you're most likely using ns-3.37. 
You can use a more recent release, or use the available workaround 

socket.Bind(ns.addressFromInetSocketAddress(your_InetSocketAddress))

tamuct ccilab

unread,
Oct 16, 2023, 3:21:46 PM10/16/23
to ns-3-users
Thank you all.
Yes, I was using ns-3.37
The workaround works to bind the socket.

socket.Bind(ns.addressFromInetSocketAddress(your_InetSocketAddress))
Reply all
Reply to author
Forward
0 new messages