如何使用 OMNeT++ 中的 INET 访问和使用接收节点中的数据包内容?
How can I access and use the packet contents in the received node using INET in OMNeT++?
在 omnet++/INET 中,我有两个节点(标准主机),通过扩展 UDPBasicApp 应用程序,我必须将传感器值发送到另一个节点。我做了自己的 .msg
定义,如下所示:
class PlantPacket extends FieldsChunk
{
double sensorValue;
}
我发送这个值没有问题,我使用了这个代码:
void MyUdp::sendPacket()
{
std::ostringstream str;
str << packetName << "-" << numSent;
Packet *packet = new Packet(str.str().c_str());
const auto& payload = makeShared<PlantPacket>();
payload->setChunkLength(B(par("messageLength")));
payload->setSensorValue(sensorValue);
payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
packet->insertAtBack(payload);
L3Address destAddr = chooseDestAddr();
socket.sendTo(packet, destAddr, destPort);
}
我的问题是在接收到的节点中检索 sensorValue
。我使用了这段代码,但出现错误:
void MyUdp::processPacket(Packet *pk)
{
EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
PlantPacket* payload = check_and_cast<PlantPacket*>(pk);
EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
delete pk;
}
Error: Cannot convert between integer units -- in module (inet::Udp)
我该如何解决这个问题?
已使用 insertAtBack
添加有效负载,因此必须使用 peekAtBack
或 popAtBack
获取。代码应如下所示:
void MyUdp::processPacket(Packet *pk)
{
EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
const auto& payload = pk->peekAtBack<PlantPacket>();
EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
delete pk;
}
在 omnet++/INET 中,我有两个节点(标准主机),通过扩展 UDPBasicApp 应用程序,我必须将传感器值发送到另一个节点。我做了自己的 .msg
定义,如下所示:
class PlantPacket extends FieldsChunk
{
double sensorValue;
}
我发送这个值没有问题,我使用了这个代码:
void MyUdp::sendPacket()
{
std::ostringstream str;
str << packetName << "-" << numSent;
Packet *packet = new Packet(str.str().c_str());
const auto& payload = makeShared<PlantPacket>();
payload->setChunkLength(B(par("messageLength")));
payload->setSensorValue(sensorValue);
payload->addTag<CreationTimeTag>()->setCreationTime(simTime());
packet->insertAtBack(payload);
L3Address destAddr = chooseDestAddr();
socket.sendTo(packet, destAddr, destPort);
}
我的问题是在接收到的节点中检索 sensorValue
。我使用了这段代码,但出现错误:
void MyUdp::processPacket(Packet *pk)
{
EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
PlantPacket* payload = check_and_cast<PlantPacket*>(pk);
EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
delete pk;
}
Error: Cannot convert between integer units -- in module (inet::Udp)
我该如何解决这个问题?
已使用 insertAtBack
添加有效负载,因此必须使用 peekAtBack
或 popAtBack
获取。代码应如下所示:
void MyUdp::processPacket(Packet *pk)
{
EV_INFO << "Received packet: " << UdpSocket::getReceivedPacketInfo(pk) << endl;
const auto& payload = pk->peekAtBack<PlantPacket>();
EV_INFO << "packet content1: " << payload->getSensorValue() << endl;
delete pk;
}