简体   繁体   English

OMNeT ++中不同类型数据包之间的冲突

[英]Conflicts between different types of packets in OMNeT++

I have created a simulation in OMNeT++ where I have one client and one server (both of them are UDPBasicApp modules). 我在OMNeT ++中创建了一个模拟,我有一个客户端和一个服务器(它们都是UDPBasicApp模块)。 The client sends packets to the server. 客户端将数据包发送到服务器。 The server also sends packets to the client, which are two subclasses of cPacket. 服务器还将数据包发送到客户端,客户端是cPacket的两个子类。

Unfortunately, there are conflicts between those two types of packets when they are received by the client. 不幸的是,当客户端接收到这两种类型的数据包时,它们之间存在冲突。 Let's assume that the types of the two packets are called FirstPacket and SecondPacket respectively (classes derived from cPacket). 假设两个数据包的类型分别称为FirstPacket和SecondPacket(从cPacket派生的类)。 By running the simulation, as soon as the client receives the first packet from the server the simulation crashes and I get someting like the following error message: 通过运行模拟,一旦客户端从服务器收到第一个数据包,模拟就会崩溃,我得到的信息如下:

"check_and_cast(): cannot cast (FirstPacket*).ClientServer.client.udpApp[0] to type SecondPacket"

How I can solve this problem? 我怎么能解决这个问题? How the server can successfully receive both types of packets sent by the client? 服务器如何成功接收客户端发送的两种类型的数据包?

You are probably using something like SecondPacket* p = check_and_cast<SecondPacket*>(pkt); 您可能正在使用类似SecondPacket* p = check_and_cast<SecondPacket*>(pkt); to force each incoming packet to be treated as being of type SecondPacket . 强制将每个传入的数据包视为SecondPacket类型。 OMNeT++'s check_and_cast will abort your simulation if this is not the case. 如果不是这样,OMNeT ++的check_and_cast将中止你的模拟。 A simple solution is to use a dynamic_cast instead: 一个简单的解决方案是使用dynamic_cast

PacketTypeA* a = dynamic_cast<PacketTypeA*>(pkt);
PacketTypeB* b = dynamic_cast<PacketTypeB*>(pkt);
if (a) {
  printf("got packet type A: %d", a->some_field_of_a);
}
if (b) {
  printf("got packet type B: %d", b->some_field_of_b);
}

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

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