简体   繁体   English

QTcpSocket写入数据结构

[英]QTcpSocket write an struct of data

I used to send an struct with C++ sockets, and now I'm trying to use QTcpSockets instead. 我曾经使用C ++套接字发送结构,现在我正尝试使用QTcpSockets。 This is the struct: 这是结构:

const unsigned int uiMessageSize = 4;
    typedef struct {
        unsigned char uc_stx;
        unsigned char uc_add;
        unsigned char uc_state;
        unsigned char uc_eot;
        unsigned char uc_unused_padding[1024];
    } st_message;

So then, for writing I used to call write(sockfd, &messageToSend, uiMessagePcToMastSize); 因此,对于写操作,我习惯调用write(sockfd, &messageToSend, uiMessagePcToMastSize);

Now I'm trying with: socket->write(&messageToSend, uiMessagePcToMastSize); 现在,我尝试使用: socket->write(&messageToSend, uiMessagePcToMastSize); and doesn't work: no matching function for call to 'QTcpSocket::write(mastControl::st_messagePcToMast*, const unsigned int&) . 并且不起作用: 没有用于调用'QTcpSocket :: write(mastControl :: st_messagePcToMast *,const unsigned int&)的匹配函数

I have searched more info, and looks like I should convert the struct to a QDataStrem, or just a QByteArray. 我搜索了更多信息,并且看起来应该将结构转换为QDataStrem或仅转换为QByteArray。 I don't really know how to del with this. 我真的不知道该如何解决。

Any help? 有什么帮助吗?

文档显示,write函数已重载了1个函数,即qint64 QIODevice::write ( const char * data, qint64 maxSize ) ,因此强制转换为const char*就足够了。

Just add a toQBytearray and add a constructor in the struct where you split the bytearray to the struct where you convert the data . 只需添加toQBytearray并在将字节数组拆分为转换数据的结构的结构中添加构造函数。 Put a splitvalue Like # or ; 放入一个像#或; between the datasets and when you receive the data on your server just use the above mentioned constructor 在数据集之间以及在服务器上接收数据时,只需使用上述构造函数

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

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