[Solved]How to check socket connect to host
-
wrote on 19 Jun 2014, 11:54 last edited by
Hi!
After
socket->connectToHost(server->text(), port->value());How can i find socket connect to host or not??
-
Hi,
If the connection succeeds "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal is emitted.
You can connect this signal to your slot. -
wrote on 19 Jun 2014, 12:32 last edited by
also can check by
@if (socket->state() == QAbstractSocket::Connected)@
-
wrote on 19 Jun 2014, 12:33 last edited by
Thanks for the reply..
I am using
if(socket->state() == QAbstractSocket::ConnectedState)
to check but it is not working.. -
wrote on 19 Jun 2014, 12:36 last edited by
What is the state value returned ?
-
wrote on 19 Jun 2014, 13:03 last edited by
if i use
if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)
it always returns true
-
wrote on 19 Jun 2014, 13:40 last edited by
Of course the statement below is always true:
@if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)
@as QAbstractSocket::ConnectingState is certainly greater then zero.
You need to change the statement to
@if((socket->state() == QAbstractSocket::ConnectedState) ||
(socket->state() == QAbstractSocket::ConnectingState))@
-
wrote on 20 Jun 2014, 05:27 last edited by
Thanks all for the reply..
First of all i am checking socket is connected using
if (socket->state() == QAbstractSocket::UnconnectedState)
it is working fine, then i add
socket->connectToHost(server->text(), port->value());
after this i want to know is it conncet or not??
Moreover, I try to use this
if (socket->state() == QAbstractSocket::connectedState)
But it is not working?
Could you please guide me?
-
Did you try connecting to the "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal ?
-
wrote on 20 Jun 2014, 13:31 last edited by
Look at the example in the documentation (fortune client)
"http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html":http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html.If you still have trouble post your code
-
wrote on 25 Jun 2014, 05:41 last edited by
Thanks!
It works..
I simply check it using condition
if(!socket->waitForConnected(1000))
{
//code
} -
wrote on 25 Jun 2014, 07:30 last edited by
If your problem is solved then please prepend your title as <solved>.
1/12