Omnet ++ / INET:从cMessage转换为Mypacket类型失败

时间:2019-05-07 01:13:15

标签: c++ tcp omnet++ inet

我正在尝试使用tcp会话将数据包从客户端发送到服务器。 我要发送的数据包的类型为“ Mypacket”,是 Omnet ++ cPacket。但是,由于某些未知原因,我在接收器上收到以下错误:

  

**无法将cPacket类型转换为Mypacket **

这是客户端的发送功能:

void TCPAppBase::sendPacket(Mypacket *msg)
{

long numBytes = msg->getByteLength();
double value =msg->getValue();
int nodeID = msg->getNodeID;
cout<< "value sent is:\n"<<  value <<"from node :\n"<< node <<"bytes are :\n"<<bytes ;
emit(sentPkSignal, msg);
socket.send(msg);

packetsSent++;
bytesSent += numBytes;
}

这是服务器的接收功能:

void TCPSinkApp::handleMessage(cMessage *msg)
{

if (msg->getKind() == TCP_I_PEER_CLOSED) {

    msg->setName("close");
    msg->setKind(TCP_C_CLOSE);
    send(msg, "tcpOut");
}
else if (msg->getKind() == TCP_I_DATA || msg->getKind() == TCP_I_URGENT_DATA) {

   Mypacket *pk= dynamic_cast< Mypacket *>(msg);
                  if (!pk){
                      throw cRuntimeError("Message (%s)%s is not  Mypacket -- "
                                       "probably wrong client app, or 
                                        wrong setting of TCP's "
                                          "dataTransferMode parameters "
                                          "(try \"object\")",
                                  msg->getClassName(), msg->getName());}



    double value = pk->getValue();
    int node = pk->getNodeID();
    long bytes = pk->getByteLength();

    cout<< "value received is:\n"<<  value <<"from node :\n"<< node <<"bytes are :\n"<<bytes ;


   bytesRcvd += bytes;

    emit(rcvdPkSignal, pk);
    delete msg;
}

这是Mypacket的定义:

packet Mypacket{ double value ;  int nodeID ;}

运行模拟时,将显示以下运行时错误:

  

**消息(cPacket)数据不是Mypacket-可能是错误的客户端应用程序,或TCP的dataTransferMode参数设置错误(尝试“对象”)**。

我不明白为什么收到的数据包是cPacket类型!我肯定是在发送Mypacket数据包。我已经为这个问题苦苦挣扎了一个星期。谁能帮忙吗?

0 个答案:

没有答案