2015-12-10 39 views
2

概述的問題:
操作系統:Ubuntu的複製煤焦與數據流中的QByteArray包含了一些額外的字節

我使用QT工具接收(遠程機器使用的GStreamer發送從遠程機器的視頻數據實時數據)並將該數據寫入端口5000.

端口5000已綁定到另一個gstreamer實用程序。此實用程序監聽端口5000並將數據轉換爲視頻流。很明顯,事情並不完全正常,我不能觀看視頻。所以我有兩個問題:

1)使用Qt實用程序,是否合法寫入端口5000,儘管端口綁定到gstreamer實用程序。

2)我正在使用第三方庫及其api接收來自外部數據源的數據。數據存儲在字符數組中。如果我將它轉換爲qbytearray,那麼qbytearray與char緩衝區的大小相同。例如

rc = zco_receive_source(sdr, &reader, bytesToRead, buffer); // this is 3rd part function 


     qDebug() << " copy buffer size =" << rc; // genrally this size is 1412 

     QByteArray msgRecvd; 

     msgRecvd = QByteArray(reinterpret_cast<char*>(buffer), rc); 

     if(! msgRecvd.isEmpty()) 
     { 
      qDebug() << " size of msgRecv =" << msgRecvd.size();// again 1412 

      udpSock->writeDatagram(msgRecvd, QHostAddress::LocalHost, 5000); 
     } 

但是,如果使用QDataStream然後QByteArray中得到了4個額外的字節。如下圖所示

rc = zco_receive_source(sdr, &reader, bytesToRead, buffer); // this is 3rd part function 


    qDebug() << " copy buffer size =" << rc; // genrally this size is 1412 

    QByteArray msgRecvd; 
    QDataStream dataStream(&msgRecvd, QIODevice::WriteOnly); 

    dataStream.writeBytes((const char*)buffer, rc); 

    if(! msgRecvd.isEmpty()) 
    { 

     qDebug() << " size of msgRecv =" << msgRecvd.size();// got 4 extra bytes .. size is 1415. why ??? 

     udpSock->writeDatagram(msgRecvd, QHostAddress::LocalHost, 5000); 
    } 

代碼,我想知道爲什麼QByteArray中得到額外的性格和我需要序列化的數據轉發給端口5000?

回答

1

回答第二個問題: 嘗試QDataStream :: writeRawData()。

形式的Qt文檔:

QDataStream & QDataStream::writeBytes(const char * s, uint len) 
Writes the length specifier len and the buffer s to the stream and returns a reference to the stream. 
The len is serialized as a quint32, followed by len bytes from s. Note that the data is not encoded. 
+0

感謝隊友。那是複製粘貼錯誤。我改變了它。 – samprat

+0

好的,更新了我的答案 – kewl

相關問題