我試圖通過boost::asio
發送5,000
個字節的char數據包。 但我得到錯誤:buffer overflow
。 有沒有辦法增強自動分割或我必須手動執行?Boost ASIO:帶有5 kb數據包的緩衝區溢出
下面是包代碼:
char * CPlayerCharacter::getEnviPacket() //len 5824
{
char * buffer = new char[5824];
char xf[4];
char yf[4];
sprintf(xf, "%f", x);
sprintf(yf, "%f", y);
for(int i =0;i<5824;i++)
{
buffer[i] = 0x00;
}
buffer[0] = 5824 & 0xff;
buffer[1] = 5824 >> 8 & 0xff;
buffer[4] = 4; //minor id
buffer[6] = 1; //major id
buffer[8] = 1;
buffer[12] = id & 0xff;
buffer[13] = (id>>8) & 0xff;
buffer[14] = (id>>16) & 0xff;
buffer[15] = (id>>24) & 0xff;
buffer[20] = mapid;//map id
buffer[24] = 18;
buffer[28] = 0x0a;
buffer[29] = 0x0f;
buffer[30] = 0x16;
for(int i =0;i<4;i++)
{
buffer[5800+i] = xf[i];
buffer[5804+i] = yf[i];
}
buffer[5808] = 0x0c;
buffer[5813] = 0x9d;
buffer[5814] = 0x0f;
buffer[5815] = 0xbf;
buffer[5822] = 0x60;
buffer[5823] = 0x2a;
return buffer;
}
下面是處理代碼:
void CConnection::handle_enter_world(int packetlen)
{
Decryptor dec;
char buffer[packetlen];
boost::asio::async_read(socket_,
boost::asio::buffer(buffer, packetlen),
boost::bind(
&CConnection::nothing, shared_from_this()));
char * decrypted = decrypted = dec.decrypt((char*)&buffer, packetlen);
asynchronousSend(pl.characters[(int)decrypted[0]].getEnviPacket(), 5824);
}
這裏是asynchronousSend功能:
void CConnection::asynchronousSend(char * data, int len)
{
boost::asio::async_write(socket_, boost::asio::buffer(data, len), boost::bind(&CConnection::handle_write, shared_from_this()));
delete [] data;
}
感謝。
這取決於您使用的代碼,但您沒有發佈_that_。 – Chad
好的,我添加了代碼。 –