简体   繁体   English

ns3数据包有效负载打印

[英]ns3 packet payload printing

I am using ns3 for a simulation where I create a packet as follows; 我正在使用ns3进行仿真,在仿真中,我创建了如下数据包;

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;

In receiver side, I am using following code to retrieve the packet payload; 在接收方,我正在使用以下代码来检索数据包有效负载;

uint8_t *buffer = new uint8_t[p->GetSize ()];
size = p->CopyData(buffer, p->GetSize ());
string s = string(buffer, buffer+p->GetSize());
cout<<"Received:"<<s<<endl;

However, the packet payload is some weird characters. 但是,数据包有效载荷是一些奇怪的字符。 Moreover, when I run the same code on my other computer with the different message (not hello world), I can see the message with weird characters at the beginning and end of the message. 此外,当我在另一台计算机上运行相同的代码但显示不同的消息(不是世界)时,可以在消息的开头和结尾看到带有奇怪字符的消息。 Any suggestion and advice are appreciated. 任何建议和意见,表示赞赏。

Regards. 问候。

I solve the problem with the help of ns-3 google groups. 我在ns-3 Google网上论坛的帮助下解决了这个问题。 I am posting the answer, in case others may need. 如果其他人可能需要,我会发布答案。

In sender side, I need to set the message length as follows; 在发送方,我需要如下设置消息长度;

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

I need to add 1 to the message length, or I skip the null string termination. 我需要在消息长度上加1,否则跳过空字符串终止。 So this one is a minor bug. 因此,这是一个小错误。 The major one is; 主要的是; I trigger a function when I receive the packet on MAC layer. 当我在MAC层上收到数据包时触发一个功能。 To properly read the packet content, I should remove the MAC headers and trailers. 为了正确读取数据包内容,我应该删除MAC标头和尾标。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM