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

Unable to read from socket if data is first written to it

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.5.0
    • Network: Sockets
    • None
    • Succeeds on:
      * Linux (Ubuntu 15.04 tested)
      * Mac OS X (10.10 tested)

      Fails on:
      * Windows (8.1 tested)
    • macOS

    Description

      Consider the following test case:

      TestSocketBug.cpp
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <QTest>
      
      class TestSocketBug : public QObject
      {
          Q_OBJECT
      
      private Q_SLOTS:
      
          void testBug();
      };
      
      void TestSocketBug::testBug()
      {
          QTcpServer server;
          QVERIFY(server.listen(QHostAddress::LocalHost));
      
          QTcpSocket clientSocket;
          clientSocket.connectToHost(server.serverAddress(), server.serverPort());
          QTRY_COMPARE(clientSocket.state(), QAbstractSocket::ConnectedState);
      
          QTcpSocket *serverSocket = server.nextPendingConnection();
          serverSocket->write("test");
          serverSocket->close();
      
          clientSocket.write("something");
      
          QTRY_COMPARE(clientSocket.bytesAvailable(), 4);
      }
      
      QTEST_MAIN(TestSocketBug)
      #include "TestSocketBug.moc"
      

      The test case can be compiled with the following build script:

      CMakeLists.txt
      cmake_minimum_required(VERSION 2.8.11)
      project(socketbug)
      
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      set(CMAKE_AUTOMOC ON)
      
      find_package(Qt5Network REQUIRED)
      find_package(Qt5Test REQUIRED)
      
      add_executable(socketbug TestSocketBug.cpp)
      qt5_use_modules(socketbug Network Test)
      

      When run, the test fails on Windows since no data is available for reading on clientSocket. However, if the clientSocket.write() line is commented out, the test passes.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            richmoore Richard Moore (qtnetwork)
            nathan-osman Nathan Osman
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes