[SOLVED] connect to the server message

  • 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);@

  • Moderators

    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.

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

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

  • Moderators

    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.

  • 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 )@

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

    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.

  • Moderators

    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

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

  • [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.

  • Moderators

    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.

  • [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 :-)

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

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

Log in to reply

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