1

我觉得这是一个愚蠢的错误,但我正在制作一个回显服务器来识别数据包,并输出数据,然后重新发送回去。它适用于某些数据包,但我试图回应的一个数据包正在中断并说有一个堆被损坏。

Vector 类,现在仅用作容器:

class vec2
{
public:
     vec2(){};
     float x, y;
};

玩家数据结构:

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 接受您从收到的 char* 中抛出的 void*,它的处理方式如下:

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++ 的新手并且喜欢学习。

4

1 回答 1

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);
    }
 };
于 2013-08-06T23:20:22.503 回答