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

XMLHttpRequest has no connection to the localhost if internet turnes off

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Incomplete
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.6.0, 5.6.1, 5.7.0
    • None

      Problem description:

      1. When you run the application with the connected Internet, you get a response from the local server:
        Application output

        qml: Iteration 1: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 1: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 1: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 1: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 2: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 2: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 2: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        qml: Iteration 2: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        ...

      2. When you run the application without Internet connection, you get a response from the local server:
        Application output

        qml: Iteration 1: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 1: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 1: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 1: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 2: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 2: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 2: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        qml: Iteration 2: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        ...

      3. If you run an application with an Internet connection and turn it off after a 4 iterations you get:
        Application output

        qml: Iteration 1: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 1: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 1: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 1: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:31 2016'
        qml: Iteration 2: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 2: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 2: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        qml: Iteration 2: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:36 2016'
        qml: Iteration 3: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 3: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 3: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:41 2016'
        qml: Iteration 3: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:41 2016'
        qml: Iteration 4: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 4: request.readyState = HEADERS_RECEIVED, request.responseText: ''
        qml: Iteration 4: request.readyState = LOADING, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:46 2016'
        qml: Iteration 4: request.readyState = DONE, request.responseText: '<h1>Nothing to see here</h1>?? ??? 27 15:59:46 2016'
        qml: Iteration 5: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 5: request.readyState = DONE, request.responseText: ''
        qml: Iteration 6: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 6: request.readyState = DONE, request.responseText: ''
        qml: Iteration 7: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 7: request.readyState = DONE, request.responseText: ''
        qml: Iteration 8: request.readyState = OPENED, request.responseText: ''
        qml: Iteration 8: request.readyState = DONE, request.responseText: ''
        ...

      After turning off Internet, access to local server possible via browser.
      If you run in parallel with the first to the second instance of the application after disabling Internet. He (second instance) will receive a response from the local server.

      My code example:

      Client sample
      /*project.pro*/
      	TEMPLATE = app
      
      	QT += qml quick
      
      	CONFIG += c++11
      
      	SOURCES += main.cpp
      
      	RESOURCES += qml.qrc
      
      	# Additional import path used to resolve QML modules in Qt Creator's code model
      	QML_IMPORT_PATH =
      
      	# Default rules for deployment.
      	qnx: target.path = /tmp/$${TARGET}/bin
      	else: unix:!android: target.path = /opt/$${TARGET}/bin
      	!isEmpty(target.path): INSTALLS += target
      
      
      /*main.cpp*/
      	#include <QGuiApplication>
      	#include <QQmlApplicationEngine>
      
      	int main(int argc, char *argv[])
      	{
      	    QGuiApplication app(argc, argv);
      
      	    QQmlApplicationEngine engine;
      	    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      
      	    return app.exec();
      	}
      
      
      /*main.qml*/
          import QtQuick 2.4
          import QtQuick.Window 2.2
          Window {
              id: root
              visible: true
      
              property int it: 0
      
              Timer {
                  id: timer;
                  interval: 5000;
                  repeat: true;
                  onTriggered: {
                      root.it++;
                      sendCommand(root.it);
                  }
                  
                  Component.onCompleted: timer.start();
              }
      
              function sendCommand(iteration) {
                  var request = new XMLHttpRequest();
                  timer.running = false;
                  
                  request.onreadystatechange = function() {
                      timer.running = true;
                      console.debug("Iteration %1: request.readyState = %2, request.responseText: '%3'"
                                    .arg(iteration)
                                    .arg(getReqState(request))
                                    .arg(request.responseText.toString()));
                  }
                  request.open('GET', 'http://localhost:33333/', false);
                  request.send(null);
              }
      
              function getReqState(request) {
                  switch(request.readyState) {
                  case XMLHttpRequest.OPENED:
                      return "OPENED";
                  case XMLHttpRequest.LOADING:
                      return "LOADING";
                  case XMLHttpRequest.HEADERS_RECEIVED:
                      return "HEADERS_RECEIVED";
                  case XMLHttpRequest.DONE:
                      return "DONE";
                  }
              }
          }
      
      Server sample
      /*server.pro*/
          QT       += core gui network
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          TARGET = untitled6
          TEMPLATE = app
          SOURCES += main.cpp\
                  mainwindow.cpp
          HEADERS  += mainwindow.h
          FORMS    += mainwindow.ui
      
      /*main.cpp*/
          #include "mainwindow.h"
          #include <QApplication>
      
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
      
              return a.exec();
          }
      
      /*mainwindow.h*/
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
      
          #include <QMainWindow>
          #include <QtNetwork>
          #include <QTcpSocket>
          #include <QObject>
          #include <QByteArray>
          #include <QDebug>
      
          namespace Ui {
              class MainWindow;
          }
      
          class QTcpServer;
      
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
      
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
      
          private slots:
              void on_starting_clicked();
              void on_stoping_clicked();
              void newuser();
              void slotReadClient();
      
          private:
              Ui::MainWindow *ui;
              QTcpServer *tcpServer;
              int server_status;
              QMap<int,QTcpSocket *> SClients;
          };
          #endif // MAINWINDOW_H
      
      /*mainwindow.cpp*/
          #include "mainwindow.h"
          #include "ui_mainwindow.h"
      
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          }
      
          MainWindow::~MainWindow()
          {
              delete ui;
              server_status=0;
          }
      
          void MainWindow::on_starting_clicked()
          {
              tcpServer = new QTcpServer(this);
              connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newuser()));
              if (!tcpServer->listen(QHostAddress::Any, 33333) && server_status==0) {
                  qDebug() <<  QObject::tr("Unable to start the server: %1.").arg(tcpServer->errorString());
                  ui->textinfo->append(tcpServer->errorString());
              } else {
                  server_status=1;
                  qDebug() << tcpServer->isListening() << "TCPSocket listen on port";
                  ui->textinfo->append(QString::fromUtf8("Server started!"));
                  qDebug() << QString::fromUtf8("Server started!");
              }
          }
      
          void MainWindow::on_stoping_clicked()
          {
              if(server_status==1){
                  foreach(int i,SClients.keys()){
                      QTextStream os(SClients[i]);
                      os.setAutoDetectUnicode(true);
                      os << QDateTime::currentDateTime().toString() << "\n";
                      SClients[i]->close();
                      SClients.remove(i);
                  }
                  tcpServer->close();
                  ui->textinfo->append(QString::fromUtf8("Server stopped!"));
                  qDebug() << QString::fromUtf8("Server stopped!");
                  server_status=0;
              }
          }
      
      
          void MainWindow::newuser()
          {
              if(server_status==1){
                  qDebug() << QString::fromUtf8("New connection!");
                  ui->textinfo->append(QString::fromUtf8("New connection!"));
                  QTcpSocket* clientSocket=tcpServer->nextPendingConnection();
                  int idusersocs=clientSocket->socketDescriptor();
                  SClients[idusersocs]=clientSocket;
                  connect(SClients[idusersocs],SIGNAL(readyRead()),this, SLOT(slotReadClient()));
              }
          }
      
          void MainWindow::slotReadClient()
          {
              QTcpSocket* clientSocket = (QTcpSocket*)sender();
              int idusersocs=clientSocket->socketDescriptor();
              QTextStream os(clientSocket);
              os.setAutoDetectUnicode(true);
              os << "HTTP/1.0 200 Ok\r\n"
                    "Content-Type: text/html; charset=\"utf-8\"\r\n"
                    "\r\n"
                    "<h1>Nothing to see here</h1>"
                    << QDateTime::currentDateTime().toString();
              ui->textinfo->append("ReadClient:"+clientSocket->readAll()+"\n\r");
      
              clientSocket->close();
              SClients.remove(idusersocs);
          }
      
      /*mainwindow.ui*/
          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class&gt;MainWindow</class&gt;
           <widget class="QMainWindow" name="MainWindow">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>796</width>
              <height>484</height>
             </rect>
            </property>
            <property name="windowTitle">
             <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralWidget">
             <layout class="QGridLayout" name="gridLayout">
              <item row="0" column="0" colspan="3">
               <widget class="QTextBrowser" name="textinfo"/>
              </item>
              <item row="1" column="0">
               <widget class="QPushButton" name="stoping">
                <property name="text">
                 <string>Stop</string>
                </property>
               </widget>
              </item>
              <item row="1" column="1">
               <spacer name="horizontalSpacer">
                <property name="orientation">
                 <enum>Qt::Horizontal</enum>
                </property>
                <property name="sizeHint" stdset="0">
                 <size>
                  <width>653</width>
                  <height>20</height>
                 </size>
                </property>
               </spacer>
              </item>
              <item row="1" column="2">
               <widget class="QPushButton" name="starting">
                <property name="text">
                 <string>Start</string>
                </property>
               </widget>
              </item>
             </layout>
            </widget>
           </widget>
           <layoutdefault spacing="6" margin="11"/>
           <resources/>
           <connections/>
          </ui>
      

        1. ClientSample.zip
          2 kB
          YURI IBETULLOV
        2. ServerSample.zip
          3 kB
          YURI IBETULLOV
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            manordheim MÃ¥rten Nordheim
            yuroro YURI IBETULLOV
            Votes:
            3 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes