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