Switch between several IP until socket state is connected



  • 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();
    }
    

  • Moderators

    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.



  • can you show me an example??


  • Lifetime Qt Champion

    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



  • 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??


  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.