Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [SOLVED] Android QT5.2 : socket->ConnectToHost() making nothing on Android device

[SOLVED] Android QT5.2 : socket->ConnectToHost() making nothing on Android device

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 2 Posters 3.7k Views
  • 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.
  • E Offline
    E Offline
    Eskay
    wrote on last edited by
    #1

    Hi,

    I'm using Qt 5.2, QtCreator 3.0, Windows8 64 bits
    I've had a look at a lot of forums, but I haven't seen the same problem with QtCreator3.0

    I'm trying to make an application to connect an Android Qt client to Desktop Qt server with WI-FI. Desktop server and android device are connected to the same Wi-Fi network.
    I manage to get the connection when I run both client and server on desktop, but it does not work on when the client runs on Android device.
    I tried on 2 android devices, Samsung Galaxy S3 and Samsumg Galaxy Ace

    The main problem is that I connected "QTcpSocket::connected()" and "QAbstractSocket::SocketError" to my applicaiton, and none of them are called after "connectToHost()" on the android device.

    Concerning permissions, i allowed those ones :
    android.permissions.ACCESS_WIFI_SATE
    android.permissions.CHANGE_WIFI_SATE
    android.permissions.INTERNET
    android.permissions.WRITE_EXTERNAL_STORAGE

    my project .pro file contains
    QT += network

    I can see logs of the "connecter" function above, but nothing neither from connected, nor from SocketError

    Here are some parts of my code

    Socket Creation :
    @
    //client
    socket = new QTcpSocket();
    connect(socket, SIGNAL(readyRead()), this, SLOT(donneesRecues()));
    connect(socket, SIGNAL(connected()), this, SLOT(connecte()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(deconnecte()));
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
    @

    Connect function :
    @
    void NetworkClient::connecter(QString p_ip,QString p_port)
    {
    qDebug() << "connexion : " << p_ip << " / "<< p_port;
    socket->abort();
    socket->connectToHost(p_ip, p_port.toUInt());
    }
    @

    Error function
    @
    void NetworkClient::erreurSocket(QAbstractSocket::SocketError erreur)
    {
    switch(erreur) // On affiche un message différent selon l'erreur qu'on nous indique
    {
    case QAbstractSocket::HostNotFoundError:
    _qmlCpp->invokeQmlFunction("log","ERREUR : le serveur n'a pas pu être trouvé. Vérifiez l'IP et le port.");
    //listeMessages->append(tr("<em>ERREUR : le serveur n'a pas pu être trouvé. Vérifiez l'IP et le port.</em>"));
    break;
    case QAbstractSocket::ConnectionRefusedError:
    _qmlCpp->invokeQmlFunction("log","ERREUR : le serveur a refusé la connexion. Vérifiez si le programme "serveur" a bien été lancé. Vérifiez aussi l'IP et le port");
    //listeMessages->append(tr("<em>ERREUR : le serveur a refusé la connexion. Vérifiez si le programme "serveur" a bien été lancé. Vérifiez aussi l'IP et le port.</em>"));
    break;
    case QAbstractSocket::RemoteHostClosedError:
    _qmlCpp->invokeQmlFunction("log","ERREUR : le serveur a coupé la connexion.");
    //listeMessages->append(tr("<em>ERREUR : le serveur a coupé la connexion.</em>"));
    break;
    default:
    _qmlCpp->invokeQmlFunction("log","ERREUR : erreur.");
    //listeMessages->append(tr("<em>ERREUR : ") + socket->errorString() + tr("</em>"));
    break;
    }

    }
    @

    Connected :
    @
    void NetworkClient::connecte()
    {
    _qmlCpp->invokeQmlFunction("connectionOk","Connection OK");
    }
    @

    I saw a post with a similar problem to mine, but the person forgot to include QtNetwork library in the package configuration. In my case, I think there is not such problems, as far as I'm using QtCreator3.0 (that apparently includes automatically libraries).

    Thank you for helping.

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eskay
      wrote on last edited by
      #2

      I solved this using the function waitForConnected() that returns a boolean.
      This is not exactly what I was looking for, but it works.

      1 Reply Last reply
      0
      • 0 Offline
        0 Offline
        00091701
        wrote on last edited by
        #3

        Hi,
        I have nearly the same problem with QT5.4.1. But it does not work for me.
        Here is my code: https://git.hoffmann-projects.de/?p=ds-qrpigpiocontrol.git;a=summary

        Can you help me?

        Thanks!

        (I use: Linux (64bit), QT5.4.1, qtcreator3)

        1 Reply Last reply
        0
        • 0 Offline
          0 Offline
          00091701
          wrote on last edited by
          #4

          Hi,
          I have nearly the same problem with QT5.4.1. But it does not work for me.
          Here is my code: https://git.hoffmann-projects.de/?p=ds-qrpigpiocontrol.git;a=summary

          Can you help me?

          Thanks!

          (I use: Linux (64bit), QT5.4.1, qtcreator3)

          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