Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket and WebAssemblity trouble

QTcpSocket and WebAssemblity trouble

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 834 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bleach
    wrote on last edited by Bleach
    #1

    Hi guys. I built QTcpSocket example
    https://github.com/manfredipist/QTcpSocket.git
    with WebAssemblity.
    Unfortunately on start I receive error
    000The following error occurred: Network operation timed out.

    Same code with MSVC build work perfect.

    An important part of the code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        socket = new QTcpSocket(this);
    
        connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage);
        connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket);
        connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket);
        connect(socket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
    
        socket->connectToHost("192.168.1.222", 8080);
    
       if(socket->waitForConnected())
            ui->statusBar->showMessage("Connected to Server");
        else{
            QMessageBox::critical(this,"QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString()));
            exit(EXIT_FAILURE);
        }
    }
    
    MainWindow::~MainWindow()
    {
        if(socket->isOpen())
            socket->close();
        delete ui;
    }
    
    void MainWindow::readSocket()
    {
        ...
    }
    
    void MainWindow::discardSocket()
    {
        socket->deleteLater();
        socket=nullptr;
    
        ui->statusBar->showMessage("Disconnected!");
    }
    
    void MainWindow::displayError(QAbstractSocket::SocketError socketError)
    {
        switch (socketError) {
            case QAbstractSocket::RemoteHostClosedError:
            break;
            case QAbstractSocket::HostNotFoundError:
                QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings.");
            break;
            case QAbstractSocket::ConnectionRefusedError:
                QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct.");
            break;
            default:
                QMessageBox::information(this, "QTCPClient", QString("000The following error occurred: %1.").arg(socket->errorString()));
            break;
        }
    }
    

    Any ideas on how to fix this?
    Thanks.

    KroMignonK 1 Reply Last reply
    0
    • B Bleach

      Hi guys. I built QTcpSocket example
      https://github.com/manfredipist/QTcpSocket.git
      with WebAssemblity.
      Unfortunately on start I receive error
      000The following error occurred: Network operation timed out.

      Same code with MSVC build work perfect.

      An important part of the code:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          socket = new QTcpSocket(this);
      
          connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage);
          connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket);
          connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket);
          connect(socket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
      
          socket->connectToHost("192.168.1.222", 8080);
      
         if(socket->waitForConnected())
              ui->statusBar->showMessage("Connected to Server");
          else{
              QMessageBox::critical(this,"QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString()));
              exit(EXIT_FAILURE);
          }
      }
      
      MainWindow::~MainWindow()
      {
          if(socket->isOpen())
              socket->close();
          delete ui;
      }
      
      void MainWindow::readSocket()
      {
          ...
      }
      
      void MainWindow::discardSocket()
      {
          socket->deleteLater();
          socket=nullptr;
      
          ui->statusBar->showMessage("Disconnected!");
      }
      
      void MainWindow::displayError(QAbstractSocket::SocketError socketError)
      {
          switch (socketError) {
              case QAbstractSocket::RemoteHostClosedError:
              break;
              case QAbstractSocket::HostNotFoundError:
                  QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings.");
              break;
              case QAbstractSocket::ConnectionRefusedError:
                  QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct.");
              break;
              default:
                  QMessageBox::information(this, "QTCPClient", QString("000The following error occurred: %1.").arg(socket->errorString()));
              break;
          }
      }
      

      Any ideas on how to fix this?
      Thanks.

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #3

      @Bleach said in QTcpSocket and WebAssemblity trouble:

      Hi guys. I built QTcpSocket example
      https://github.com/manfredipist/QTcpSocket.git
      with WebAssemblity.
      Unfortunately on start I receive error
      000The following error occurred: Network operation timed out.
      Same code with MSVC build work perfect.

      AFAIK QtWebAssembly does not support QTcpSocket, only QWebSocket are possible (sandboxing issues).

      • https://wiki.qt.io/Qt_for_WebAssembly
      • https://doc.qt.io/qt-5/qtwebassembly-platform-notes.html#known-limitations

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      3
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Does some process listening on 192.168.1.222:8080?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        B 1 Reply Last reply
        1
        • B Bleach

          Hi guys. I built QTcpSocket example
          https://github.com/manfredipist/QTcpSocket.git
          with WebAssemblity.
          Unfortunately on start I receive error
          000The following error occurred: Network operation timed out.

          Same code with MSVC build work perfect.

          An important part of the code:

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              socket = new QTcpSocket(this);
          
              connect(this, &MainWindow::newMessage, this, &MainWindow::displayMessage);
              connect(socket, &QTcpSocket::readyRead, this, &MainWindow::readSocket);
              connect(socket, &QTcpSocket::disconnected, this, &MainWindow::discardSocket);
              connect(socket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
          
              socket->connectToHost("192.168.1.222", 8080);
          
             if(socket->waitForConnected())
                  ui->statusBar->showMessage("Connected to Server");
              else{
                  QMessageBox::critical(this,"QTCPClient", QString("The following error occurred: %1.").arg(socket->errorString()));
                  exit(EXIT_FAILURE);
              }
          }
          
          MainWindow::~MainWindow()
          {
              if(socket->isOpen())
                  socket->close();
              delete ui;
          }
          
          void MainWindow::readSocket()
          {
              ...
          }
          
          void MainWindow::discardSocket()
          {
              socket->deleteLater();
              socket=nullptr;
          
              ui->statusBar->showMessage("Disconnected!");
          }
          
          void MainWindow::displayError(QAbstractSocket::SocketError socketError)
          {
              switch (socketError) {
                  case QAbstractSocket::RemoteHostClosedError:
                  break;
                  case QAbstractSocket::HostNotFoundError:
                      QMessageBox::information(this, "QTCPClient", "The host was not found. Please check the host name and port settings.");
                  break;
                  case QAbstractSocket::ConnectionRefusedError:
                      QMessageBox::information(this, "QTCPClient", "The connection was refused by the peer. Make sure QTCPServer is running, and check that the host name and port settings are correct.");
                  break;
                  default:
                      QMessageBox::information(this, "QTCPClient", QString("000The following error occurred: %1.").arg(socket->errorString()));
                  break;
              }
          }
          

          Any ideas on how to fix this?
          Thanks.

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by KroMignon
          #3

          @Bleach said in QTcpSocket and WebAssemblity trouble:

          Hi guys. I built QTcpSocket example
          https://github.com/manfredipist/QTcpSocket.git
          with WebAssemblity.
          Unfortunately on start I receive error
          000The following error occurred: Network operation timed out.
          Same code with MSVC build work perfect.

          AFAIK QtWebAssembly does not support QTcpSocket, only QWebSocket are possible (sandboxing issues).

          • https://wiki.qt.io/Qt_for_WebAssembly
          • https://doc.qt.io/qt-5/qtwebassembly-platform-notes.html#known-limitations

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            Does some process listening on 192.168.1.222:8080?

            B Offline
            B Offline
            Bleach
            wrote on last edited by Bleach
            #4

            @Christian-Ehrlicher No.

            @KroMignon Thanks. I'll try to use QWebSocket.

            1 Reply Last reply
            0
            • lorn.potterL Offline
              lorn.potterL Offline
              lorn.potter
              wrote on last edited by
              #5

              Qt Webassembly cannot block the main thread, so normal Qt socket classes still do not work correctly, as is. There is some work being done to get them working better with a threaded version of Qt WebAssembly, but it is still fairly WIP and not scheduled for any Qt version yet. Even so, some things like select() and poll() will not work.

              Best to use websockets, as any posix sockets get translated into websockets, unless you use a proxy server method mentioned in:

              https://emscripten.org/docs/porting/networking.html

              Freelance Software Engineer, Platform Maintainer QtWebAssembly, Maintainer QtSensors
              Author, Hands-On Mobile and Embedded Development with Qt 5 http://bit.ly/HandsOnMobileEmbedded

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved