2012-05-03 57 views
0

我以前用C++套接字發送一個結構,現在我試圖用QTcpSockets來代替。這是結構:QTcpSocket寫入一個數據結構

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; 

那麼,寫我習慣叫write(sockfd, &messageToSend, uiMessagePcToMastSize);

現在,我試圖用:socket->write(&messageToSend, uiMessagePcToMastSize);並不起作用:沒有匹配函數調用「與QTcpSocket: :write(mastControl :: st_messagePcToMast *,const unsigned int &)

我搜索了更多的信息,看起來像我應該將結構轉換爲QDataStrem,或者只是一個QByteArray。我真的不知道如何解決這個問題。

任何幫助?

+0

此鏈接可能有幫助,http://stackoverflow.com/questions/2570679/serialization-with-qt – Kunal

回答

1

documentation顯示寫入函數過載1函數爲qint64 QIODevice::write (const char * data, qint64 maxSize),因此投射到const char*應該足夠了。

0

只需添加一個toQBytearray並在結構中添加一個構造函數,將字節數組分割爲轉換數據的結構體。放置一個splitvalue像#或;在數據集之間以及在服務器上收到數據時,只需使用上述構造函數