Qt TCP/IP socket connection check -
i writing qt tcp/ip client. want check connection state server before send data sever. far knowledge can following methods
- use bool 'connectionstate', set variable when connected sever , reset variable on disconnected() signal. before sending data server (client->write()) check value of variable.
- use 'client->state() == qtcpsocket::connectedstate' way check connection state.
which practice. or other method this.
thanks in advance.
qtcpsocket
derived qabstractsocket
, provides state()
function. returns 1 of following enums: -
enum socketstate { unconnectedstate, hostlookupstate, connectingstate, connectedstate, ..., listeningstate }
so, assuming m_psocket qtcpsocket
, check if connected:-
bool connected = (m_psocket->state() == qtcpsocket::connectedstate);
you add boolean , keep track of state, if network error occurs need ensure in-sync actual connection state.
Comments
Post a Comment