Testing TCP / IP Qt Sockets

I am writing a Qt TCP / IP client. I want to check the status of the connection to the server before sending data to the server. As far as I know, I can do this by following the methods

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

This is a good practice. Or any other method for this.

Thanks in advance.

+4
source share
1 answer

QTCPSocket QAbstractSocket, state(). : -

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

, m_pSocket QTCPSocket, , , : -

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

, , , .

+7

Source: https://habr.com/ru/post/1530371/


All Articles