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 NetworkError with Wifi OK but Internet NOK
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket NetworkError with Wifi OK but Internet NOK

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 7 Posters 3.7k Views 2 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.
  • SGaistS SGaist

    Do you mean when there's a client connecting to it ?
    Or when you start listening ?

    Just thinking out loud, do you have a firewall setup when your device is in AP mode ?

    Match0umM Offline
    Match0umM Offline
    Match0um
    wrote on last edited by Match0um
    #13

    @SGaist The issue occurs when the client tries to connect to the server :

    • In the case where it works (ethernet cable plugged in the PC (client)) : If the client tries to connect to the server while the server is not listening, the client hits something like QAsbtractSocket::ConnectionRefusedError or QAbstractSocket::SocketTimeoutError.
    • But in the case where it doesn't work (client doesn't have access to the internet), it hits QAbstractSocket::NetworkError without any delay.

    I don't have any firewall parameter on my raspberry. Here is the AP config (hostapd.conf) :

    country_code=FR
    interface=wlan0
    ssid=wifiAP
    hw_mode=g
    channel=7
    wmm_enabled=0
    macaddr_acl=0
    ignore_broadcast_ssid=1
    auth_algs=1
    wpa=2
    wpa_passphrase=passwrd
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP
    

    What I was thinking about, maybe the MAC address (or another parameter) of my PC or my raspberry is not authorized on the other device. I had connected the raspberry to the same ethernet network once few weeks ago. Maybe it changed something to the settings ?

    1 Reply Last reply
    0
    • Match0umM Offline
      Match0umM Offline
      Match0um
      wrote on last edited by Match0um
      #14

      I can confim you guys that the issue comes from the client side.

      With my client offline (so no ethernet cable plugged in), I can connect the PC to the raspberry in SSH with Putty or cmd.
      So in Qt, I decided to change the port number to 22 (ssh) in _client->connectToHost(ip, port 22). The behavior is exactly the same as explained before. It fails when the client is offline, it works when the client is online.
      So the issue comes from Qt, or my code. But I don't know what.

      1 Reply Last reply
      0
      • Match0umM Match0um

        Hello !

        After a few days of digging, here is the situation:
        I use QTCPSocket to transfer datas from a Windows client to a Linux system server.

        The server is configured as a Wifi AP without internet connection.

        When the client is connected to the server through Wifi, without internet, I hit NetworkError as soon as I try send/receive datas.

        But if the client is additionnaly connected to internet through ethernet, everything works perfectly.

        Can someone helps me / explain me the issue please ? ?

        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #15

        @Match0um said in QTcpSocket NetworkError with Wifi OK but Internet NOK:

        I hit NetworkError

        Could you please share the error? Specially the error string...

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        Match0umM 1 Reply Last reply
        0
        • Pablo J. RoginaP Pablo J. Rogina

          @Match0um said in QTcpSocket NetworkError with Wifi OK but Internet NOK:

          I hit NetworkError

          Could you please share the error? Specially the error string...

          Match0umM Offline
          Match0umM Offline
          Match0um
          wrote on last edited by
          #16

          @Pablo-J-Rogina Hi, here is how I manage errors (I added the error string) :

          connect(_client, SIGNAL(error(QAbstractSocket::SocketError)), this SLOT(error(QAbstractSocket::SocketError)));
          
          void myClass::error(QAbstractSocket::SocketError error)
          {
              qDebug() << "ERROR " << error;
              qDebug()<< "Error string "<<_client.errorString();
          }
          

          So in the App output I get :
          "ERROR QAbstractSocket::NetworkError".
          Error string "Host unreachable"

          I also ran my code in debug mode but I have no additionnal information about the error :/

          Pablo J. RoginaP 1 Reply Last reply
          0
          • Match0umM Offline
            Match0umM Offline
            Match0um
            wrote on last edited by
            #17

            Ok guys, I finally found what causes the problem :

            It is in my method connectionWifi() :

            void MyClass::connectionWifi()
            {
                _connectionAttempts++;
                qDebug()<<"@@ Connection wifi";
                QString resultDisk;
                QProcess process;
                int returnCode = process.execute("netsh wlan connect name=wifiAp");
            
                process.start("netsh wlan show networks");
                process.waitForFinished();
                resultDisk = process.readAllStandardOutput();
            
                if(returnCode==0){
                    if(resultDisk.contains("wifiAp")){
                        ui->label_statutWifi->setText(tr("Détecté"));
                        connection();
                    }else{
                        ui->label_statutWifi->setText(tr("Non connecté"));
                        ui->label_connect->setText(tr("Outil non détecté"));
                    }
            
                }else{
                    ui->label_statutWifi->setText(tr("Wifi de la tablette éteind"));
                    ui->label_connect->setText(tr("Activez le WiFi"));
                }
            }
            

            If I comment this line it works well :

            int returnCode = process.execute("netsh wlan connect name=wifiAp");
            

            I don't know why this line makes the code not working when the the ethernet cable is unplugged (the ethernet network has no link with the wifi network I am trying to connect to).

            JonBJ 1 Reply Last reply
            0
            • Match0umM Match0um

              Ok guys, I finally found what causes the problem :

              It is in my method connectionWifi() :

              void MyClass::connectionWifi()
              {
                  _connectionAttempts++;
                  qDebug()<<"@@ Connection wifi";
                  QString resultDisk;
                  QProcess process;
                  int returnCode = process.execute("netsh wlan connect name=wifiAp");
              
                  process.start("netsh wlan show networks");
                  process.waitForFinished();
                  resultDisk = process.readAllStandardOutput();
              
                  if(returnCode==0){
                      if(resultDisk.contains("wifiAp")){
                          ui->label_statutWifi->setText(tr("Détecté"));
                          connection();
                      }else{
                          ui->label_statutWifi->setText(tr("Non connecté"));
                          ui->label_connect->setText(tr("Outil non détecté"));
                      }
              
                  }else{
                      ui->label_statutWifi->setText(tr("Wifi de la tablette éteind"));
                      ui->label_connect->setText(tr("Activez le WiFi"));
                  }
              }
              

              If I comment this line it works well :

              int returnCode = process.execute("netsh wlan connect name=wifiAp");
              

              I don't know why this line makes the code not working when the the ethernet cable is unplugged (the ethernet network has no link with the wifi network I am trying to connect to).

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #18

              @Match0um said in QTcpSocket NetworkError with Wifi OK but Internet NOK:

              int returnCode = process.execute("netsh wlan connect name=wifiAp");

              Try unplugging ethernet cable and run netsh wlan connect name=wifiAp from a Command Prompt. What happens, does it block?

              Match0umM 1 Reply Last reply
              0
              • JonBJ JonB

                @Match0um said in QTcpSocket NetworkError with Wifi OK but Internet NOK:

                int returnCode = process.execute("netsh wlan connect name=wifiAp");

                Try unplugging ethernet cable and run netsh wlan connect name=wifiAp from a Command Prompt. What happens, does it block?

                Match0umM Offline
                Match0umM Offline
                Match0um
                wrote on last edited by
                #19

                @JonB It does not block when I run this command, I get the output " the connection request was successful"

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #20

                  Then try again in your code but building the parameter list rather than passing the complete command as a string.

                  I guess there's an issue with the = part.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Match0umM 1 Reply Last reply
                  0
                  • Kent-DorfmanK Offline
                    Kent-DorfmanK Offline
                    Kent-Dorfman
                    wrote on last edited by
                    #21
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • SGaistS SGaist

                      Then try again in your code but building the parameter list rather than passing the complete command as a string.

                      I guess there's an issue with the = part.

                      Match0umM Offline
                      Match0umM Offline
                      Match0um
                      wrote on last edited by Match0um
                      #22

                      @SGaist Did you mean something like that ? :

                      QString cmd = "netsh";
                      QStringList arguments;
                      arguments <<"wlan"<<"connect"<<"name=mScan";
                      int returnCode = process.execute(cmd, arguments);
                      

                      It doesn't change the behavior, I have the output "the connection request was successful", it connects my PC to the wifi access point, but if I run this commend the connectToHost method hits NetworkError.

                      JonBJ 1 Reply Last reply
                      0
                      • Match0umM Match0um

                        @SGaist Did you mean something like that ? :

                        QString cmd = "netsh";
                        QStringList arguments;
                        arguments <<"wlan"<<"connect"<<"name=mScan";
                        int returnCode = process.execute(cmd, arguments);
                        

                        It doesn't change the behavior, I have the output "the connection request was successful", it connects my PC to the wifi access point, but if I run this commend the connectToHost method hits NetworkError.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #23

                        @Match0um
                        Yes, that is what @SGaist intended. We wanted to to verify that the element name=mScan was passed as a single argument.

                        Whatever the issue is, it must be related, somehow, to the consequences of (successfully) running the command, not the act of running the command itself.

                        1 Reply Last reply
                        0
                        • Match0umM Match0um

                          @Pablo-J-Rogina Hi, here is how I manage errors (I added the error string) :

                          connect(_client, SIGNAL(error(QAbstractSocket::SocketError)), this SLOT(error(QAbstractSocket::SocketError)));
                          
                          void myClass::error(QAbstractSocket::SocketError error)
                          {
                              qDebug() << "ERROR " << error;
                              qDebug()<< "Error string "<<_client.errorString();
                          }
                          

                          So in the App output I get :
                          "ERROR QAbstractSocket::NetworkError".
                          Error string "Host unreachable"

                          I also ran my code in debug mode but I have no additionnal information about the error :/

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by
                          #24

                          @Match0um said in QTcpSocket NetworkError with Wifi OK but Internet NOK:

                          Error string "Host unreachable"

                          This is a routing issue. You may want to capture and analyze network traffic (i.e. using Wireshark) to see what's going on under your Qt app depending on what connections (wireless or wired) you have on your client PC

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0

                          • Login

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