Вопрос

I used to send an struct with C++ sockets, and now I'm trying to use QTcpSockets instead. 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);

Now I'm trying with: socket->write(&messageToSend, uiMessagePcToMastSize); and doesn't work: no matching function for call to ‘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. I don't really know how to del with this.

Any help?

Это было полезно?

Решение

the documentation shows that the write function is overloaded with 1 function being qint64 QIODevice::write ( const char * data, qint64 maxSize ), so casting to const char* should be enough.

Другие советы

Just add a toQBytearray and add a constructor in the struct where you split the bytearray to the struct where you convert the data . 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 с атрибуция
Не связан с StackOverflow
scroll top