7

I am writing Qt TCP/IP client. I want to check the connection state with server before send data to sever. As far my knowledge I can do this by following methods

  • Use a bool 'ConnectionState', set this variable when connected with sever and reset this variable on disconnected() signal. Now before sending data to server (client->write()) check the value of this variable.
  • use this 'client->state() == QTcpSocket::ConnectedState' way to check the connection state.

Which is good practice. Or any other method to this.

Thanks In advance.

beparas
  • 1,927
  • 7
  • 24
  • 30
  • One possible approach is described in [this answer](https://stackoverflow.com/a/10447560/514235). But that's not Qt way. @beparas, did creating a slot for `error(SocketError)` signal work for you. For me it's not working as described in the accepted answer. – iammilind Jun 28 '17 at 11:26

2 Answers2

15

QTCPSocket is derived from QAbstractSocket, which provides a state() function. This returns one of the following enums: -

enum SocketState { UnconnectedState, HostLookupState, ConnectingState, ConnectedState, ..., ListeningState }

So, assuming m_pSocket is a QTcpSocket, you would simply do this to check if it is connected:-

bool connected = (m_pSocket->state() == QTcpSocket::ConnectedState);

You could add a boolean and keep track of the state, but if a network error occurs you need to ensure that it is always in-sync with the actual connection state.

TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
  • 1
    Connect a slot to the error signal: void error(QAbstractSocket::SocketError socketError) – TheDarkKnight Mar 07 '14 at 09:28
  • I have connected a slot to the `error(SocketError)` signal, but that is never invoked even when the Wifi is switched off. Following statements doesn't have any effect on this: `setSocketOption(QAbstractSocket::LowDelayOption, true)` and `setSocketOption(QAbstractSocket::KeepAliveOption, true)`. Is there anything am I missing? – iammilind Jun 28 '17 at 11:02
  • @iammilind, I suggest asking this as a separate question on SO, with a [MCVE](https://stackoverflow.com/help/mcve) – TheDarkKnight Jun 28 '17 at 11:54
  • [Qt: Why is `QAbstractSocket::error(QAbstractSocket::SocketError)` signal not generated when internet is disconnected?](https://stackoverflow.com/q/44803126/514235) – iammilind Jun 28 '17 at 12:57
0

You can use errorOccurred signal and It's just enough for this signal define a slot in client. when an error occurs, a signal trigger and you can receive notify in slot function.

In client.h

/* define a slot for client */
public slots:
    void errorOccurred(QAbstractSocket::SocketError error);

In client.c

/*client constructor*/
Client::Client(QObject *parent) {
/*some other code here*/
    connect(socket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
        this, SLOT(errorOccurred(QAbstractSocket::SocketError)));

/*and maybe some other code here*/
}

and in client.c write implementation for errorOccurred:

void Client::errorOccurred(QAbstractSocket::SocketError error) {
    qDebug() << "error in connection: " << socket->errorString();
}

OUTPUT:

error in connection:  "Connection refused"