引入库
find_package(ZLIB REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC ZLIB::ZLIB)
#include <zlib.h>
bool calculateAndCheckCRC32(const char* data, size_t dataSize, unsigned int crc32_recv)
{
if (!data)
{
std::cout << "data is empty " << std::endl;
return false;
}
unsigned int crc32_calc = crc32(0L, Z_NULL, 0);
crc32_calc = crc32(crc32_calc, (const Bytef*)data, dataSize);
printf("Src crc32: %u calculate crc32: %u \n", crc32_recv, crc32_calc);
if (crc32_recv == crc32_calc)
{
std::cout << "CRC32校验成功" << std::endl;
return true;
}
else
{
std::cout << "CRC32校验失败" << std::endl;
return false;
}
}
tool_class tc;
const std::string requestUrl = "http://127.0.0.1:5000/?v=1&b=0&s=120&p=text";
std::string response = tc.sendHttpRequest(requestUrl);
if (response.size()>4)
{
int size = response.size();
std::cout << "Received data size: " << size << std::endl;
unsigned int fileSize;
memcpy(&fileSize, &response[0], 4);
std::cout << "fileSize: " << fileSize << std::endl;
unsigned int crc32_rec;
memcpy(&crc32_rec, &response[size - 4], 4);
std::cout << "crc32_rec: " << crc32_rec << std::endl;
calculateAndCheckCRC32(response.data() + 4, response.size() - 8, crc32_rec);
}