How can I get the string context from a received packet?? CopyData and *packet both failed

90 views
Skip to first unread message

C.Wu

unread,
May 11, 2016, 11:42:20 PM5/11/16
to ns-3-users
Hello, everyone!
I am working on WSN simulations. Now I have encountered a problem with packet. In my simulation, I need to make every node send a broadcast packet to its neighbor node. In the packet, I would like to write some information into its buffer like:
stringstream pktContents("Hello");
Ptr<Packet> pkt = Create<Packet>((uint8_t *)pktContents.str().c_str(), pktContents.str().length());
and add an ipv4 header to record the destination, source and identification:
Ipv4Header ipv4Header;
ipv4Header.SetDestination(senseIfs.GetAddress(0));
ipv4Header.SetSource(sinkIf.GetAddress(0));
ipv4Header.SetIdentification(1);
pkt->AddHeader(ipv4Header);
Then, when the destination node received the packet, there is a callback to analyze the socket. In the callback, I can get the packet in the socket. But there comes the problem that I cannot access the contents("Hello") in any ways I know, such as CopyData, *packet: 
When I use *packet like:
cout<<"Contents: "<<*packet<<endl
<<"Size: "<<packet->GetSize()<<"Bytes"<<", including "<<h.GetSerializedSize()<<"Bytes "<<h.GetTypeId()<<std::endl
I get nothing:
Contents: 
Size: 5Bytes, including 20Bytes ns3::Ipv4Header
If I use CopyData, there comes an error:
ostream *os;
packet->CopyData(os, packet->GetSize ());
The error information indicates os uninitialized, I don't know how I should initialize it.

Thank you so much for your help and support.

Best wishes

Wu Chao

Tommaso Pecorella

unread,
May 12, 2016, 3:53:28 AM5/12/16
to ns-3-users

C.Wu

unread,
May 12, 2016, 3:56:43 AM5/12/16
to ns-3-users
Thanks, I've solved the problem by using ostringstream when CopyData(ostream *, uint32_t)
Reply all
Reply to author
Forward
0 new messages