packet payload weird chracters

655 views
Skip to first unread message

batgun

unread,
Apr 20, 2017, 8:30:03 AM4/20/17
to ns-3-users
Dear All;

I am working on WPAN and try to send a packet with content. As the ns3 tutorials suggest, I am using payload
rather than header solution. So I create the packet as follows;

 //Sending Packets
 
McpsDataRequestParams params;
 
params.m_srcAddrMode = SHORT_ADDR;
 
params.m_dstAddrMode = SHORT_ADDR;
 
params.m_dstPanId = 0;
 
params.m_dstAddr = Mac16Address ("FF:FF");
 
params.m_msduHandle = 0;
 
params.m_txOptions = TX_OPTION_ACK;

 
string packetInformation = ("");
 //generatepacket() returns string content
 packetInformation
= generatePacket();
 stringstream msgx
;
 msgx
<< packetInformation;
 uint16_t packetSize
= packetInformation.length();
 
Ptr<Packet> packet = Create<Packet>((uint8_t*) msgx.str().c_str(), packetSize);

 Simulator::ScheduleWithContext (1, Seconds (0.0),
&LrWpanMac::McpsDataRequest,
firstNode->device->GetMac (), params, packet);

Nodes in the first node's transmission range can receive the packet. I follow the MacRx event and I get the 
packet's payload as follow;

 // p is the received packet
 cout
<<"Packet Size:"<<p->GetSize()<<endl;
 uint8_t
*buffer = new uint8_t[p->GetSize ()];
 p
->CopyData (buffer, p->GetSize ());
 
string receivedData(buffer, buffer+p->GetSize ());
 cout
<<"Received:"<<receivedData<<endl;

However, when I run the simulation, the received data is something like this  �x. I search on ns-3 google groups but I cannot fix this.
Any advice or suggestion is appreciated. 

Kind regards.

batgun

unread,
Apr 21, 2017, 4:02:43 AM4/21/17
to ns-3-users
Dear All;

I also tried the following codes;

Sender:


 std
::ostringstream msg; msg << "Hello World!" << '\0';
 
Ptr<Packet> packet = Create<Packet> ((uint8_t*) msg.str().c_str(), msg.str().length());
 packet
->Print (cout);
 std
::cout << std::endl;

Receiver:
 uint8_t *buffer = new uint8_t[p->GetSize ()];
 p
->CopyData(buffer, p->GetSize ());

 
string s = string((char*)buffer);
 cout
<<"Received:"<<s<<endl;


Still, I can see the weird characters on console. May be the error is related with
the simple c++ thing. Any advice or suggestion is appreciated.

Regards.

Tommaso Pecorella

unread,
Apr 24, 2017, 9:53:43 AM4/24/17
to ns-3-users
Hi,

you're bringing ONLY weird chars or the weird cars are at the end of the ones that you meant to transmit ?

T.

batgun

unread,
Apr 24, 2017, 10:42:30 AM4/24/17
to ns-3-users
Dear Tommaso;

Thanks for your consideration. As summary, I am trying to simulate the WPAN from the example that ns3 
provided namely lr-wpan-example-skript.cc. By using this file and codes, I want to send a specific message 
within the Packet. To implement this, I use the previous post's code samples. I have a string message and I
use packet payload to transmit the message.

I have two computers one Linux and one Mac. When I tried to get the buffer content by using the packet copyData  
in MacRX event on Linux, I see weird characters ( �x) as output.  On the other hand, when I run same code on my
Mac, I see weird characters at the beginning and end of the message where the message is in the middle.

Kind regards.

Tommaso Pecorella

unread,
Apr 24, 2017, 1:53:40 PM4/24/17
to ns-3-users
Hi,

can you attach the script ?

T.

batgun

unread,
Apr 25, 2017, 1:51:28 AM4/25/17
to ns-3-users
Dear Tommaso;

Please find the script on attachment. The codes are written based on the example
lr-wpan-example-skript.cc.  I just modify the creating packet (with payload) and the 
receive event (copy data into buffer).  Thanks for your help and consideration.

Regards.
packetPayloadTest.cc

Tommaso Pecorella

unread,
Apr 25, 2017, 9:35:44 AM4/25/17
to ns-3-users
Hi,

it was easier than I thought. You made some little but important mistakes.

Let's start with a minor one.
 uint16_t packetSize = msgx.str().length();

 
Ptr<Packet> packet = Create<Packet>((uint8_t*) msgx.str().c_str(), packetSize);

There's a bug in the above code. You have to add 1 to the message length, or you'll skip the null string termination.
The "bug" will be seen when you'll read the packet content later on.
As a general rule using null-terminated strings is a very bad idea. Use a TLV encoding instead. 

Now the "real" one.
 dev->GetMac()->TraceConnectWithoutContext ("MacRx", MakeBoundCallback(&PacketReceived, dev));
this will connect to the low-level packet reception, and the packet will be the one you receive at MAC level, UNPROCESSED.
Practically... with the MAC header and trailer !

You was printing the MAC header, not the packet content.
Remove them and all will work. Of course you'll need to copy the packet and work on the copy, but that's easy.


T.

batgun

unread,
Apr 25, 2017, 10:58:48 AM4/25/17
to ns-3-users
Dear Tommaso;

Thanks for your reply. I copy data packet and remove the headers. Now I can 
see the packet content. Thanks for your informative explanation.

Regards.
Message has been deleted
Message has been deleted

Asayel A

unread,
Mar 22, 2019, 10:00:33 AM3/22/19
to ns-3-users
Greetings,

can you please tell me how did you copy the packet data and remove the header then print the packet content?

your help would really be appreciated.


On Thursday, April 20, 2017 at 3:30:03 PM UTC+3, batgun wrote:

Erick Joel Zherdmant Cevallos

unread,
Jun 6, 2019, 11:14:08 PM6/6/19
to ns-3-users
Hi, I have this code and I try to send a information  packet, but when I try to print this information just after receive  it  by the receiving node, in the display it show me this (��U), and it does not show the real information sent from emitting node. How can I fix it?. Any idea, Thank you.
lr-wpan-txrx.cc
Reply all
Reply to author
Forward
0 new messages