2013-08-06 12 views
1

我有一种感觉这是一个愚蠢的错误,但我正在制作回声服务器来识别数据包,并输出数据,然后重新发回它。它适用于某些数据包,但我试图回应的一个数据包正在崩溃,并说有一个堆被损坏。运行到与WinSock2应用程序堆腐败

Vector类,只是用集装箱现在:

​​

PlayerData结构:

struct PlayerData 
{ 
    vec2 pos; 
    int health; 
    float rotation; 
    char moveflags; 
    short playerID; 
}; 

包我想送:

struct ServerPacket_SyncGame //5 
{ 
    short packetID; 
    PlayerData data[8]; 
}; 

接下来的部分是凌乱,但我会评论它尝试是有道理的。

ServerPacket_SyncGame* SP_SG = new ServerPacket_SyncGame; //creates packet pointer 
for(int i = 0; i < 8; i++) //assigns the eight playerdata structs in the packet array 
{ 
    SP_SG->data[i].playerID = i; 
    SP_SG->data[i].health = rand() % 30; 
    SP_SG->data[i].moveflags = 'D'; 
    SP_SG->data[i].pos.x = rand() % 1000; 
    SP_SG->data[i].pos.y = rand() % 1000; 
    SP_SG->data[i].rotation = rand() % 360; 
} 
SP_SG->packetID = 5; //assigns the packet id 

cout << "\n\nSent data: \n"; ////Outputting the data to be sent 
for(int i = 0; i < 8; i++) 
    cout << "\nPlayer ID: " << SP_SG->data[i].playerID << "\nPosition: (" 
     << SP_SG->data[i].pos.x << ", " << SP_SG->data[i].pos.y 
     << ")\nHealth: " << SP_SG->data[i].health << "\nRotation: " 
     <<SP_SG->data[i].rotation << "\nMove Flags: " 
     << SP_SG->data[i].moveflags << endl; 

void* SP_SG_DAT = (void*)SP_SG; //casting the packet into a void* 

char* SP_SG_BUFF = (char*)SP_SG_DAT; //casting from a void* to a char* 

send(Socket, SP_SG_BUFF, sizeof(ServerPacket_SyncGame), 0); //sends the char* 

char* SP_SG_RCVBUFF = new char; //new buffer for recv 

recv(Socket, SP_SG_RCVBUFF, sizeof(ServerPacket_SyncGame), 0); //recv new buffer 

void* SP_SG_RCVDAT = (void*) SP_SG_RCVBUFF; //casts char* to void* again 

ServerPacket_SyncGame* RCVSP_SG = (ServerPacket_SyncGame*) SP_SG_RCVDAT; 
//casts from void* to packet* 

cout << "\n\nRecieved Data:\n\n"; //outputs converted received information 
for(int i = 0; i < 8; i++) 
    cout << "\nPlayer ID: " << SP_SG->data[i].playerID << "\nPosition: (" 
     << SP_SG->data[i].pos.x << ", " << SP_SG->data[i].pos.y 
     << ")\nHealth: " << SP_SG->data[i].health << "\nRotation: " 
     <<SP_SG->data[i].rotation << "\nMove Flags: " 
     << SP_SG->data[i].moveflags << endl; 

我用这个方法与其他数据包,它完美地工作,服务器端这是怎么了回声:

for(;;) 
    { 
      char* buffer = new char; 
      char* temp = new char; 
      int size = recv(Socket, buffer, sizeof(ServerPacket_SyncGame), 0); 
      memcpy(temp, buffer, size); 
      send(Socket, (char*)InterpretInfo((void*)temp), size, 0); 
    }; 

InterpretInfo接受你施放煤焦出无效* *您收到,它处理它是这样的:

void* InterpretInfo(void* data) 
{ 

    short* tempsht = static_cast<short*>(data); 
    cout << "\n\nRecieved packet ID: " << *tempsht; 

    switch(*tempsht) 
    { 

这个特定包的ID是5,这是它的情况:

case 5: 
     //ServerPacket_SyncGame 
     { 
      cout << " which is ServerPacket_SyncGame\n"; 
      ServerPacket_SyncGame* decoded = (ServerPacket_SyncGame*)data; 
      for(int i = 0; i < 8; i++) 
      { 
      cout << "Player ID: " << decoded->data[i].playerID ; 
      cout << "\nPosition: (" << decoded->data[i].pos.x << ", " 
          << decoded->data[i].pos.y << ")\n"; 
      cout << "Health: " << decoded->data[i].health 
          << "\nRotation: " << decoded->data[i].rotation 
       << "\nMove Flags: " << decoded->data[i].moveflags << endl; 
      } 
      return(void*)decoded; 
    } 

它只对这个数据包起作用,当我尝试访问数据包中的任何内容时,它会中断并说堆已损坏,但在调试模式下,我可以清楚地读取数据包中的所有信息。

我至少需要10代表张贴图片,所以这里是我在运行的代码谈论的链接: http://i.imgur.com/Dbyi0c3.png

谢谢你在先进的任何帮助或洞察力,以帮助我完成这件事,我仍然是C++的新手,并且很喜欢学习。

回答

0

您只为recv()缓冲区分配了1个字节,但您正尝试读入sizeof(ServerPacket_SyncGame)字节数。你需要改变这一点:

char* SP_SG_RCVBUFF = new char; //new buffer for recv 

要这样:

char* SP_SG_RCVBUFF = new char[sizeof(ServerPacket_SyncGame)]; 

for循环同样的事情:

for(;;) 
{ 
    //char* buffer = new char; 
    char* buffer = new char[sizeof(ServerPacket_SyncGame)]; 
    //char* temp = new char; 
    char* temp = new char[sizeof(ServerPacket_SyncGame)]; 
    ... 
}; 

我建议你清理你的代码:

ServerPacket_SyncGame* SP_SG = new ServerPacket_SyncGame; //creates packet pointer 
for(int i = 0; i < 8; i++) //assigns the eight playerdata structs in the packet array 
{ 
    SP_SG->data[i].playerID = i; 
    SP_SG->data[i].health = rand() % 30; 
    SP_SG->data[i].moveflags = 'D'; 
    SP_SG->data[i].pos.x = rand() % 1000; 
    SP_SG->data[i].pos.y = rand() % 1000; 
    SP_SG->data[i].rotation = rand() % 360; 
} 
SP_SG->packetID = 5; //assigns the packet id 
... 
// don't forget to do error handling on this, and pay attention to the 
// return value so you know if you actually sent the entire struct or not... 
send(Socket, (char*)SP_SG, sizeof(ServerPacket_SyncGame), 0); 
delete SP_SG; 

SP_SG = new ServerPacket_SyncGame; 

// don't forget to do error handling on this, and pay attention to the 
// return value so you know if you actually received the entire struct or not... 
recv(Socket, (char*)SP_SG, sizeof(ServerPacket_SyncGame), 0); //recv new buffer 
... 
delete SP_SG; 

ServerPacket_SyncGame buffer; 
for(;;) 
{ 
    // don't forget to do error handling on this, and pay attention to the 
    // return value so you know if you actually received the entire struct or not... 
    int size = recv(Socket, &buffer, sizeof(ServerPacket_SyncGame), 0); 
    if (size > 0) 
    { 
     // don't forget to do error handling on this, and pay attention to the 
     // return value so you know if you actually sent the entire struct or not... 
     send(Socket, (char*)InterpretInfo(&buffer), size, 0); 
    } 
}; 
+0

非常感谢你,它现在有效。也谢谢你提出清理我的代码的建议!我真的需要他们。 –