QTcpSocket escribe una estructura de datos

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.

¿Alguna ayuda?

preguntado el 03 de mayo de 12 a las 10:05

2 Respuestas

La documentación shows that the write function is overloaded with 1 function being qint64 QIODevice::write ( const char * data, qint64 maxSize ), so casting to const char* debería ser suficiente.

contestado el 03 de mayo de 12 a las 10:05

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

contestado el 03 de mayo de 12 a las 10:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.