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. [SOLVED] connect to the server message
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] connect to the server message

Scheduled Pinned Locked Moved General and Desktop
14 Posts 4 Posters 4.2k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    in this code the user connects to the server. how to tell if the user can't connect to the server. I would like a message that displays can't connect to the server when the user fails to connect. i have tried serverError and errorString to no success.

    @ login->show();
    QSettings settings("config.ini", QSettings::IniFormat);
    serverAddress = settings.value("serverAddress").toString();
    userName = settings.value("userName").toString();

    socket = new QTcpSocket(this);
    
    this->connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    this->connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    
    socket->connectToHost(serverAddress, 4200);@
    
    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      The socket state might be checked with "state":http://doc.qt.nokia.com/4.7/qabstractsocket.html#state It delivers these "return values":http://doc.qt.nokia.com/4.7/qabstractsocket.html#SocketState-enum

      The connect statement provide also a return value to allow checking success or failure of connect.

      Vote the answer(s) that helped you to solve your issue(s)

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

        See "QAbstractSocket":http://doc.qt.nokia.com/latest/qabstractsocket.html#details, "QAbstractSocket::error()":http://doc.qt.nokia.com/latest/qabstractsocket.html#error-2.

        bq. At any time, QAbstractSocket has a state (returned by state()). The initial state is UnconnectedState. After calling connectToHost(), the socket first enters HostLookupState. If the host is found, QAbstractSocket enters ConnectingState and emits the hostFound() signal. When the connection has been established, it enters ConnectedState and emits connected(). If an error occurs at any stage, error() is emitted. Whenever the state changes, stateChanged() is emitted. For convenience, isValid() returns true if the socket is ready for reading and writing, but note that the socket's state must be ConnectedState before reading and writing can occur.

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

          i am a bit overwhelmed reading the documents and i don't know where to begin. can i have an example please.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            @
            bool boo = connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
            if ( !boo )
            {
            qDebug() << "cannot connect first signal " << endl;
            }
            boo = connect(socket, SIGNAL(connected()), this, SLOT(connected()));
            if ( !boo )
            {
            qDebug() << "cannot connect second signal " << endl;
            }

            if ( socket->connectToHost(serverAddress, 4200) == QAbstractSocket::ConnectedState )
            {
                 qDebug() << "we are connected ;-) " << endl;
            }
            

            @

            The states are given "here":http://doc.qt.nokia.com/4.7/qabstractsocket.html#SocketState-enum
            Max. 15 lines to read.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #6

              I am getting an error with this code. error: no match for 'operator==' in '((MainWindow*)this)->MainWindow::socket->QTcpSocket::<anonymous>.QAbstractSocket::connectToHost(((const QString&)((const QString*)(& serverAddress))), 4200, QFlagsQIODevice::OpenModeFlag((QIODevice::OpenModeFlag)3u)) == (QAbstractSocket::SocketState)3u'

              @if ( socket->connectToHost(serverAddress, 4200) == QAbstractSocket::ConnectedState )@

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                Read "QAbstractSocket::connectToHost":http://doc.qt.nokia.com/latest/qabstractsocket.html#connectToHost and then read
                "QAbstractSocket::waitForConnected":http://doc.qt.nokia.com/latest/qabstractsocket.html#waitForConnected.

                Just begin reading the documentation of classes you are trying to use. Every class has a "Detailed Description" section which is read in a few minutes and provides all the information needed to use this class - including related information and examples.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  Lukas is completely right. Start to read the documentation. Both of us have pointed you to the right docs. Reading them would have been much faster for you.

                  [Edit] Part eliminated, was still wrong

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #9

                    thank you Lukas Geyer for the links. I solved this issue.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on last edited by
                      #10

                      [quote author="koahnig" date="1313655049"]Lukas is completely right. Start to read the documentation. Both of us have pointed you to the right docs. Reading them would have been much faster for you.

                      Yes, you are right. The source I have provided in a hurry cannot compile.
                      It should probably more look like this.
                      @
                      socket->connectToHost(serverAddress, 4200);
                      if ( socket->state() == QAbstractSocket::ConnectedState )
                      {
                      qDebug() << "we are connected ;-) " << endl;
                      }
                      @

                      However, this is not tested and just typed out of memory. All the links provided shall give the rest. [/quote]

                      The snippet is wrong. You need either to return to the event loop or use waitForConnected.

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

                        @peppe
                        Yes, now I see it. Was puzzled that there was no return value, but did not follow through.
                        Sorry for causing the confusion. It is not always the best to bridge recompilation time with giving advises. :-(
                        Sorry again.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on last edited by
                          #12

                          [quote author="kalster" date="1313655101"]thank you Lukas Geyer for the links. I solved this issue.[/quote]

                          You're welcome.

                          Beeing a developer sometimes means just to sit there and read documentation. I know this is boring most of the time but this is what I had to learn the hard way and I guess this is what we all had to learn.

                          The good thing with Qt is that its documentation is a prime example how it has to be done and it can be clearly stated as a reference when it comes to docmentation quality.

                          And everyone how hasn't had to read and study datasheets for MCUs developed and documented somewhere in a Taiwanese backyard, hundreds of pages long, half of the information missing and half of the information wrong should count oneself lucky anyway :-)

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kalster
                            wrote on last edited by
                            #13

                            yes Lukas, i should have read the documents earlier. the problem i had was with the documents statements. It was taken me a while to read the documents and understanding the statements. hopefully, i don't have problems with the documents in the future.

                            1 Reply Last reply
                            0
                            • L Offline
                              L Offline
                              lgeyer
                              wrote on last edited by
                              #14

                              If there are any basic questions which prevent you from understanding most of the documentation feel free to ask. That's way better than having tons of specific questions and stumbling from one problem to another.

                              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