recv() 获取损坏的数据
recv() gets corrupted data
我正在尝试从C++应用程序中的C#服务器接收大数据(约7MB)。我使用此库来执行此操作:https://github.com/DFHack/clsocket但是当我收到它时,我得到严重损坏的数据。这是我如何获得它的代码:
unsigned char* image_data = client->ReadBytes(lmi_reply);
lmi_reply
是我想要接收的确切数据大小。 ReadBytes
:
uint8* Client::ReadBytes(int r) {
try {
uint8* data = new uint8(r);
this->m_s->Receive(r, data); // m_s is the CActiveSocket object.
return data;
}
catch (...) {
return 0;
}
}
我做错了什么?
附言当我同时使用 C# 客户端和服务器时,数据与服务器上的数据完全相同。
我通过将我的 ReadBytes 更改为以下内容来解决此问题:
uint8* Client::ReadBytes(int r) {
try {
char* data = new char[r];
memset(data, 0, r);
int maxBufferSize = 8192;
auto bytesReceived = decltype(r){0};
while (r > 0)
{
const auto bytesRequested = (r > maxBufferSize) ? maxBufferSize : r;
const auto returnValue = recv(this->m_s->GetSocketDescriptor(), data + bytesReceived, bytesRequested, 0);
if (returnValue == -1 || returnValue == 0)
return (uint8*)data;
bytesReceived += returnValue;
r -= returnValue;
}
return (uint8*)data;
}
catch (...) {
return 0;
}
}
相关文章:
- 为什么C中的通用链表中存储的数据已损坏
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 类数组正在损坏数据
- 带有zip_source_buffer的libzip会导致数据损坏和/或segfault
- 发送特定字节模式时串行端口数据损坏
- 放置在外部 DLL 中的类中的字符串数据的内存损坏
- Winapi ReadFile返回损坏的数据
- 向量元素数据损坏了Find()操作
- Boost 的数据驱动测试的联接运算符"+"损坏了第一列
- 为什么链表中的数据在嵌套函数中会更改/损坏?
- 写入文本文件的数据部分损坏且无法恢复
- C++和Python之间的数据损坏管道
- 内存中的数据损坏
- 如何在不存在任何数据损坏风险的情况下序列化对象
- 使用 IOCP 的 TCP/IP 服务器.接收缓冲区中偶尔的数据损坏
- 尝试读入内存时检查磁盘上文件的二进制数据损坏情况
- 文件结构,以避免数据损坏
- boost::async_write导致数据损坏
- 链接方法会导致数据损坏
- 通过复制捕获Lambda函数会导致数据损坏