2013-08-06 29 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

非常感謝你,它現在有效。也謝謝你提出清理我的代碼的建議!我真的需要他們。 –