简体   繁体   English

在客户端Winsock中没有收到完整的数据

[英]Not receiving full data in client Winsock

I have a problem in winsock client-server when sending the data. 发送数据时,我在winsock客户端服务器中有问题。 I have a map containing a username as Primary Key, and a vector of messages for each user (stored in structs): map<std::string,std::vector<message *> > data; 我有一个包含用户名作为主键的映射,以及每个用户的消息向量(存储在结构中): map<std::string,std::vector<message *> > data;

struct message{
        static unsigned int last_id;
        unsigned int id;
        std::string baa;
        std::string timestamp;
    }

I also have a serializer in order to send it to the client via Winsock (that only accepts arrays of chars) 我也有一个序列化程序,以便通过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;
    }
}

And to implement this serializer, I make the following: 为了实现此序列化程序,我进行以下操作:

message msg_cpy=*data[recvbuf_usrn].at(0);
MessageSerializer serializer(msg_cpy);
char* buffer = new char[serializer.RequiredBufferSize()];
serializer.Serialize(buffer);

The problem comes when sending the timestamp. 发送时间戳时出现问题。 In msg_cpy I have the data correctly stored(eg id=1, msg=hello, timestamp=2016-04-02 10:40:45), but when sending the data, the timestamp only saves the first three values, that is to say, in the client, I receive after deserializing id=1, msg=hello, and timestamp=201 followed by trash. msg_cpy我正确存储了数据(例如,id = 1,msg = hello,timestamp = 2016-04-02 10:40:45),但是在发送数据时,时间戳仅保存前三个值,即例如,在客户端中,我将id = 1,msg = hello和timestamp = 201反序列化后再接收垃圾邮件。 I know the problem resides in the server, and I deduce it can be the serializing function (the only other alternative could be the "send" function of Winsock). 我知道问题出在服务器上,我推断出它可能是序列化功能(唯一的其他选择可能是Winsock的“发送”功能)。 But what could be wrong in it? 但是,这可能有什么问题呢? If I write a msg=2016-04-02 10:40:20, I receive that in the client. 如果我写一个msg = 2016-04-02 10:40:20,我会在客户端收到该消息。

Any help is appreciated 任何帮助表示赞赏

The code you showed is correct. 您显示的代码是正确的。 The error is is the code you do not show. 错误是您未显示的代码。

With the 2 classes message and MessageSerializer , I tried that simple test: 使用2类messageMessageSerializer ,我尝试了一个简单的测试:

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;
}

The result is as expected 结果符合预期

 (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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM