Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-61097

Issue with operator<<(QDataStream &out, const QByteArray &ba) with QLocalSocket on Windows

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • P2: Important
    • None
    • 5.8.0
    • Core: I/O, Network: Sockets
    • None
    • Windows 10 Home. Qt 5.8.0. 32-bit Visual Studio 2015. Release build.

    Description

      I may have found a bug that involves operator<<(QDataStream &out, const QByteArray &ba) with QLocalSocket on Windows.

       

      The problem occurs when you do something like this:

      QLocalSocket localSocket;
      localSocket.connectToServer("serverName");
      
      QDataStream dataStream(&localSocket);
      
      QByteArray data("Some kind of data");
      
      dataStream << data;
      

      As far as I can tell, operator<<(QDataStream &out, const QByteArray &ba) is supposed to generate a single packet from the byte array and send it through the datastream. The packet starts with a quint32 (for the size of the data), and then the rest of the packet is the data. This works perfectly fine on Linux and Mac.

       

      However, on Windows, I have noticed that it sends two packets instead of one: one packet has the size and the other packet has the data. If this was intentional, you would think that the receiving side would be able to read it just fine. As in, on the receiving QLocalSocket:

      QByteArray receivedPacket;
      dataStream >> receivedPacket;
      

      receivedPacket should be the equivalent to "data" in the prior code section. But instead, it fails to read it and ends up being an empty byte array (.size() returns 0, and printing it with qDebug() doesn't display anything). Also, two packets are received for every one sent (and both come out to be empty). If, when the packet is received, I call QLocalSocket::readAll(), I can see that the first packet looks indeed like the quint32 size, and the second packet is the data.

      My current workaround for Windows that seems to work is as follows:

        QByteArray byteArray;
        QDataStream tmpStream(&byteArray, QIODevice::WriteOnly);
        tmpStream << data;
        socketDataStream.writeRawData(byteArray, byteArray.size());
      

      As in, I still use operator<<(QDataStream &out, const QByteArray &ba), but I use it to write to a QByteArray first (so it doesn't send two packets), and then I write the raw data from the QByteArray into the socket data stream. This solves my problem and works perfectly.

       

      I have attached some sample code for you to try out. It contains a CMakeLists.txt for compiling instructions, but you really just need to compile client.cpp for the client executable and server.cpp for the server executable. I have simplified them for understanding (because I first encountered the problem in two much larger programs).

      If you want to try it, run server.exe, and then in another terminal, run client.exe. client.exe will send a packet to server.exe, and server.exe will print out what it receives.

      When client.exe runs, this is what prints out:

      About to send a packet!
      Data to be sent is of size  65
      Data to be sent is:  "{\n    \"contents\": \"stuff\",\n    \"id\": \"1\",\n    \"jsonrpc\": \"2.0\"\n}\n"
      

      When server.exe receives it, this is what prints out:

      Accepting socket connection request on server!
      We have received a packet!
      Packet size is:  4
      Packet contents is:  "\x00\x00\x00""A"
      We have received a packet!
      Packet size is:  65
      Packet contents is:  "{\n    \"contents\": \"stuff\",\n    \"id\": \"1\",\n    \"jsonrpc\": \"2.0\"\n}\n"

      You can see that two packets are received. One looks like the size and the other looks like the data. I believe both should be received as a single packet.

       

      Anyways, let me know what you think. Thanks.

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-61097
          # Subject Branch Project Status CR V

          Activity

            People

              manordheim Mårten Nordheim
              psavery Patrick Avery
              Votes:
              2 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes