c++qtqdatastreamqudpsocket

copying char to QbyteArray with datastream contains some extra bytes


Overview of problem:
OS : Ubuntu

I am using qt utility to receive video data from remote machine( remote machine is using gstreamer to send live data) and write that data to port say 5000.

Port 5000 is already bind to another gstreamer utility. this utility listen to port 5000 and convert data into video streaming. Obviously things are not exactly working and I cant view video. So I have two questions:

1) With Qt utility , is it legal to write to port 5000 although port is bind to gstreamer utility.

2) I am using 3rd party library and its api to receive data from external source. The data get stored in array of characters. If I convert that into qbytearray then qbytearray has same size as char buffer. example

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 );
       }

But if I use QDataStream then QbyteArray got 4 extra bytes. code shown below

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 ); 
    }

I want to know why QbyteArray got extra character and do I need to serialise data to forward it to port 5000?


Solution

  • Answering on second question: Try QDataStream::writeRawData().

    Form Qt docs:

    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.