2015-12-10 55 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

相关问题