未在客户端Winsock中接收到完整数据
Not receiving full data in client Winsock
发送数据时,winsock客户端服务器出现问题。我有一个包含用户名作为主键的映射,以及每个用户的消息向量(存储在structs中):map<std::string,std::vector<message *> > data;
struct message{
static unsigned int last_id;
unsigned int id;
std::string baa;
std::string timestamp;
}
我还有一个串行器,以便通过Winsock(只接受字符数组)将其发送到客户端
class MessageSerializer
{
public:
MessageSerializer(const message& messageStruct)
: m_msgRef(messageStruct)
, m_msgLength(m_msgRef.msg.length())
, m_timeLength(m_msgRef.timestamp.length())
{}
size_t RequiredBufferSize() const
{
return sizeof(int) + sizeof(size_t)*2 + m_msgLength + m_timeLength;
}
void Serialize(void* buffer) const
{
PushNum (buffer, m_msgRef.id);
PushString (buffer, m_msgRef.msg.c_str(), m_msgLength);
PushString (buffer, m_msgRef.timestamp.c_str(), m_timeLength);
}
private:
const message& m_msgRef;
const size_t m_msgLength;
const size_t m_timeLength;
template<typename INTEGER>
void PushNum(void*& buffer, INTEGER num) const
{
INTEGER* ptr = static_cast<INTEGER*>(buffer);
//copying content
*ptr = num;
//updating the buffer pointer to point the next position to copy
buffer = ++ptr;
}
void PushString(void*& buffer, const char* cstr, size_t length) const
{
PushNum(buffer, length);
//copying string content
memcpy(buffer, cstr, length);
//updating the buffer pointer to point the next position to copy
char* ptr = static_cast<char*>(buffer);
ptr += length;
buffer = ptr;
}
}
为了实现这个序列化程序,我做了以下操作:
message msg_cpy=*data[recvbuf_usrn].at(0);
MessageSerializer serializer(msg_cpy);
char* buffer = new char[serializer.RequiredBufferSize()];
serializer.Serialize(buffer);
发送时间戳时出现问题。在msg_cpy
中,我正确地存储了数据(例如,id=1,msg=hello,timestamp=2016-04-02 10:40:45),但在发送数据时,时间戳只保存前三个值,也就是说,在客户端中,我在反序列化id=1、msg=hello和timestamp=201之后接收到垃圾。我知道问题出在服务器上,我推断它可能是序列化函数(唯一的其他选择可能是Winsock的"send"函数)。但这里面可能出了什么问题?如果我写了一条消息=2016-04-02 10:40:20,我会在客户端收到。
如有任何帮助,我们将不胜感激。
您显示的代码是正确的。错误是您没有显示的代码。
对于两个类message
和MessageSerializer
,我尝试了一个简单的测试:
int main() {
message msg = {1, "foo", "2016-04-02 10:40:20" };
MessageSerializer msgSer(msg);
size_t sz = msgSer.RequiredBufferSize();
char * buffer = new char[sz];
msgSer.Serialize(static_cast<void *>(buffer));
for (int i=0; i<sz; i++) {
std::cout << buffer[i] << " (" << std::hex << static_cast<unsigned int>(buffer[i]) << ") ";
}
std::cout << std::endl;
delete[] buffer;
return 0;
}
结果如预期
(1) (0) (0) (0) (3) (0) (0) (0) f (66) o (6f) o (6f) (13) (0) (0) (0) 2 (32) 0 (30) 1 (31) 6 (36) - (2d) 0 (30) 4 (34) - (2d) 0 (30) 2 (32) (20) 1 (31) 0 (30) : (3a) 4 (34) 0 (30) : (3a) 2 (32) 0 (30)
相关文章:
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 整数数据如何以位为单位存储在内存中?不是右对齐吗?
- 什么是 16 字节有符号整数数据类型?
- 将整数的二进制数据转换为浮点数
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- C++11 中是否有实际的 4 位整数数据类型
- 错误 C2864:'element::next':只能在类 (STRUCT) 中初始化静态常量整数数据成员
- 1 字节整数数据类型
- 使用 MFC C++ 6.0 从 ms 访问中读取整数数据
- 需要帮助从文件中获取字符和整数数据
- 在整数数据类型和连续两个字符类型之后.第 2 个字符的数据类型跳过..为什么
- C++字符串 DD:HH:MM:SS 到整数数据类型
- 如何存储具有重复格式的整数数据
- 如何从二进制文件中读取位整数数据
- 布尔数据类型vs整数数据类型来表示真值
- 将2d数组整数数据从c++发送到qml
- 如果我将数字存储为整数数据类型,如何检查另一个数字中是否存在一个数字
- 超过 C++ 的最大整数数据类型