Sending array as payload using udp echo in udp client-server example

418 views
Skip to first unread message

ns3 beginner

unread,
May 16, 2022, 4:54:27 PM5/16/22
to ns-3-users
Hi
I want to transfer some data (a python list of list of different dimensions) using udp echo (client server)  example. 
Using a given sample code in  example (copied below), I can send the data for normal cases like hellow world or simple array. To send a list of list of different dimensions (e.g. [ [1,2,3,4], [1,2] ]). I cannot use these examples. I tried converting list to string and then transfer to server, but on receiver  end, the integrity of list structure is disturbed and i can get indivual characters of the string. Sample is below.
I understand it is more like a python problem, but in ns3, is there a way to send this list as file using tcp or udp or any other simple way.
##########################
  client.SetFill (apps.Get (0), "Hello World");

  client.SetFill (apps.Get (0), 0xa5, 1024);

  uint8_t fill[] = { 0, 1, 2, 3, 4, 5, 6};
  client.SetFill (apps.Get (0), fill, sizeof(fill), 1024);
##########################

Thank you
Asif

actual data sent
'[array([[-0.45, -0.433, 0.64,\n  0.21, 0.18 ,\n -0.29 , 0.04],\n [-0.41, 0.36, 0.35,\n 0.09, 0.38 ,\n 0.30, -0.10] ]], dtype=float32), array([-0.83 , 0. , -0.40, 0.95 , 1.11 ,\n 0.21, -0.29, 0. , -0.42 , -1.16 ,\n 0.93 , 0.55 ], dtype=float32))]'

data is received at server and then tried to convert from string to list 
['[', 'a', 'r', 'r', 'a', 'y', '(', '[', '[', '-', '0', '.', '4', '5', ',',

Tommaso Pecorella

unread,
May 18, 2022, 3:54:32 PM5/18/22
to ns-3-users
Hi,

tricky question - simple answer: no, there isn't.
However, you're lucky... because also the question is wrong, and if you ask the right question, then the answer will be different.

The "right" question is: how do I save a bunch of structured data (e.g., a Python list of lists) so that it can be stored in a file or transmitted over the Internet?
Then the answer is "totally doable, you have to serialize it", see for example https://docs.python-guide.org/scenarios/serialization/
That's for Python, for C++ you'll have similar stuff - and if you need to serialize in a language and deserialize in another you'll need a "compatible" representation.

Anyway, serialization is the trick, as you need to tell the receiver what object you did send, how many sub-objects, their types (float, integers, strings, etc), and their values.

Mind that some formats add a lot of extra data, so... choose wisely. Do some experiments and find the one that suits your needs.
Reply all
Reply to author
Forward
0 new messages