[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);@
-
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.
-
@
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
"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.
-
[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.
-
[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 :-)
-