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

Socket options return strange values

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3: Somewhat important
    • Resolution: Done
    • Affects Version/s: 4.7.4, 4.8.0
    • Fix Version/s: 4.8.x, 5.0.0
    • Component/s: Network: Sockets
    • Labels:
      None
    • Environment:
      Windows 7 x64, Ubuntu 11.10 x64
    • Commits:
      3a57243fbffb771c0991c99d93ac9b9fcf4fc4ee (4.8), 46e4a9d5231e2d9e35424259858713ca539b8e30 (5.0)

      Description

      On Windows 7 with Qt 4.8, socket options for KeepAlive and LowDelay always return value -256. It doesn't change when we enable or disable this option.
      On Ubuntu 11.10 with Qt 4.7.4, these options always return 0.

      Maybe I'm doing something wrong, but here's a code to test it. It creates a server and a client socket, which firstly connects with disabled options and checks their values, then it does the same with enabled options. You can copy the code below or download the files from attachment.

      // connector.h
      
      #ifndef CONNECTOR_H
      #define CONNECTOR_H
      
      #include <QObject>
      #include <QHostAddress>
      class QTcpServer;
      class QTcpSocket;
      
      class Connector : public QObject
      {
          Q_OBJECT
      public:
          explicit Connector(QObject *parent = 0);
      
      public slots:
          void listen (QHostAddress address, int port);
          void setOptionsEnabled(bool enabled);
          void connectToHost (QString host, int port);
          void disconnectFromHost();
          
      private:
          QTcpSocket* clientSocket;
          QTcpServer* server;
          bool optionsEnabled;
      
      private slots:
          void connected();
      };
      
      #endif // CONNECTOR_H
      
      
      
      // connector.cpp
      
      #include <QTcpServer>
      #include <QTcpSocket>
      #include "connector.h"
      
      Connector::Connector (QObject *parent) :
          QObject(parent)
      {
      }
      
      void Connector::setOptionsEnabled (bool enabled)
      {
          optionsEnabled = enabled;
      }
      
      void Connector::listen (QHostAddress address, int port)
      {
          server = new QTcpServer(this);
          server->listen(address, port);
      }
      
      void Connector::connectToHost (QString host, int port)
      {
          clientSocket = new QTcpSocket(this);
          clientSocket->setSocketOption(QAbstractSocket::KeepAliveOption, optionsEnabled ? 1 : 0);
          clientSocket->setSocketOption(QAbstractSocket::LowDelayOption, optionsEnabled ? 1 : 0);
          connect (clientSocket, SIGNAL(connected()), this, SLOT(connected()));
          clientSocket->connectToHost(host, port);
          clientSocket->waitForConnected(-1);
      }
      
      void Connector::disconnectFromHost()
      {
          clientSocket->disconnectFromHost();
          if (clientSocket->state() != QAbstractSocket::UnconnectedState)
              clientSocket->waitForDisconnected(-1);
          delete clientSocket;
      }
      
      void Connector::connected()
      {
          qDebug() << "keep alive" << clientSocket->socketOption(QAbstractSocket::KeepAliveOption) << "\n"
                   << "low delay" << clientSocket->socketOption(QAbstractSocket::LowDelayOption) << "\n";
      }
      
      
      
      // main.cpp
      
      #include <QtCore/QCoreApplication>
      #include <QHostAddress>
      #include "connector.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Connector connector;
          connector.listen(QHostAddress::Any, 8888);
          qDebug() << "options set to false";
          connector.setOptionsEnabled(false);
          connector.connectToHost("localhost", 8888);
      
          connector.disconnectFromHost();
          qDebug() << "options set to true";
          connector.setOptionsEnabled(false);
          connector.connectToHost("localhost", 8888);
      
          return a.exec();
      }
      
      
      
      // socket-options-test.pro
      
      QT       += core network
      QT       -= gui
      
      TARGET = socket-options-test
      CONFIG   += console
      CONFIG   -= app_bundle
      
      TEMPLATE = app
      
      SOURCES += main.cpp \
          connector.cpp
      
      HEADERS += \
          connector.h
      

        Attachments

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

          Activity

            People

            Assignee:
            shkearns Shane Kearns
            Reporter:
            fazer Arkadiusz Piekarz
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes