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. Switch between several IP until socket state is connected
Forum Updated to NodeBB v4.3 + New Features

Switch between several IP until socket state is connected

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.0k 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    I have program and work fine when I set one IP for connect to host , I want to switch between several IP when socket not connected
    -connecting to IP in case1 if not connect , connecting to case2 and else.......
    but every time just connect to case1. how can I fix this???
    clinet.cpp:

    client::client(QWidget *parent) :
        QMainWindow(parent)
    {
       
    }
    
    
    client::~client()
    {
        _socket->disconnectFromHost();
    }
    
    void client::doconnect()
    {
        _socket = new QTcpSocket(this);
        _timer= new QTimer(this);
        qDebug()<<"connecting....";
      connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(connectionError(QAbstractSocket::SocketError)));
        connect(_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(tcpSocketState(QAbstractSocket::SocketState)));
        connect(_socket, SIGNAL(disconnected()), this, SLOT(discond()));
        connect(_socket, SIGNAL(readyRead()),this, SLOT(GetTCPData()));
      // QHostAddress address ("172.19.19.111");
        QString add;
           switch(1){
                case 1:
                 add="172.19.19.107";
                  break;
    
                case 2:
                  add="172.19.19.110";
                  break;
    
                case 3:
                  add="172.19.19.120";
                  break;
    
                default:
                 add="172.19.19.120";
                  break;
              }
    
       _socket->connectToHost(add,8585);
        qDebug()<<_socket->state();
    
    
    }
    void client::discond()
    {
        qDebug() << "disconnected...";
        _socket->deleteLater();
        _timer->singleShot(1000,this,SLOT(doconnect()));
    }
    void client::tcpSocketState(QAbstractSocket::SocketState socketState)
    {
    
        switch (socketState) {
        case QAbstractSocket::UnconnectedState:
            qDebug()<<"The socket is not connected.";
            break;
        case QAbstractSocket::HostLookupState:
            qDebug()<<"The socket is performing a hostname lookup.";
            break;
        case QAbstractSocket::ConnectedState:
            qDebug()<<"A connection is established.";
            break;
        case QAbstractSocket::ConnectingState:
            qDebug()<<"The socket has started establishing a connection.";
            break;
        case QAbstractSocket::BoundState:
            qDebug()<<"The socket is bound to an address and port.";
            break;
        case QAbstractSocket::ClosingState:
            qDebug()<<"The socket is about to close.";
            break;
        case QAbstractSocket::ListeningState:
            qDebug()<<"The socket is listening.";
            break;
        default:
            qDebug()<<"Unknown State.";
        }
    }
    
    void client::connectionError(QAbstractSocket::SocketError socketerror)
    {
        std::string errorStr = "ERROR: " + _socket->errorString().toStdString();
        if(_socket->error()==QAbstractSocket::ConnectionRefusedError || _socket->error()==QAbstractSocket::SocketResourceError)
            _timer->singleShot(1000,this,SLOT(doconnect()));
    
    
    }
    
    **main.cpp:**
    using namespace std;
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        client cln;
        cln.doconnect();
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2
      switch(1)?!
      

      what is this? It does not make any sense here.

      What you should do is: try to connect to the first IP. If it fails try to connect to the next one and so on.
      To do so you could have an integer variable in client class. Initialize it with zero and use it in the switch statement in doconnect. Each time the connection does not succeed you increment this variable and call doconnect again.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        can you show me an example??

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

          Hi,

          You already have on in your own code: switch (socketState)

          You need to store that "state" in a variable and change it when appropriated before calling doconnect

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

          1 Reply Last reply
          0
          • I Offline
            I Offline
            isan
            wrote on last edited by isan
            #5

            you mean in my this part of code

            void client::tcpSocketState(QAbstractSocket::SocketState socketState)
            {
            
                switch (socketState) {
            

            .
            .
            .

            }
            I should change this void function to int and then use return of this function ???then what??

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

              No, I mean that you already used a switch in the correct manner in that function.

              In doconnect , as @jsulm noted, you pass a constant to switch, so it will always call the same case. You have to use a variable that you update when appropriated before calling doConnect.

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

              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