Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TcpSocket stateChanged signal is not emit



  • Hi.

    I use Tcp in an application. (No special features.)
    That application is the client, and another application is the server.

    I ran the application before running the server application.
    Then client application's socket was not emit [stateChanged].
    However, It was able to connect to the server and send/receive data.

    If the server is up and running, [stateChanged] is emit.
    Why is this?


  • Lifetime Qt Champion

    @w-tkm I don't understand: if the server is not running how can your client connect and send/receive data?!

    Also, what daes this mean: "I ran the application before running the server application.( after running the server application )"? Do you start client before or after server? Please explain clearly.



  • @jsulm sorry I didn't explain myself better. I start client before server.


  • Lifetime Qt Champion

    @w-tkm Can you show your code?



  • @jsulm

    void CcuTcp::fnTcpClientSet(QString sTcpConnectIP, quint16 uiTcpConnectPort, QString sTcpBindIP)
    {
    
        connect(m_pTcpSocket, SIGNAL(readyRead()), this, SLOT(fnTcpRecvData()));
        connect(m_pTcpSocket, &QAbstractSocket::stateChanged, this, &CcuTcp::fnTcpStateChanged );
    
        m_pTcpSocket->setProxy(QNetworkProxy::NoProxy);
        m_pTcpSocket->bind( QHostAddress( sTcpBindIP ), QAbstractSocket::DefaultForPlatform);
        m_pTcpSocket->connectToHost( sTcpConnectIP, uiTcpConnectPort );
    }
    
    void CcuTcp::fnTcpStateChanged()
    {
        switch ( m_pTcpSocket->state() ) {
        case QAbstractSocket::UnconnectedState:	emit fnSocketState(TCP_STATE_UNCONNECT);	break;
        case QAbstractSocket::ConnectingState:	emit fnSocketState(TCP_STATE_CONNECTING);	break;
        case QAbstractSocket::ConnectedState:	emit fnSocketState(TCP_STATE_CONNECTED);	break;
        case QAbstractSocket::ClosingState:		emit fnSocketState(TCP_STATE_CLOSING);		break;
        default: break;
        }
    }
    

    [TCP_STATE_...] is Macro.


  • Lifetime Qt Champion

    @w-tkm said in TcpSocket stateChanged signal is not emit:

    case QAbstractSocket::UnconnectedState: emit fnSocketState(TCP_STATE_UNCONNECT); break;
    case QAbstractSocket::ConnectingState: emit fnSocketState(TCP_STATE_CONNECTING); break;
    case QAbstractSocket::ConnectedState: emit fnSocketState(TCP_STATE_CONNECTED); break;
    case QAbstractSocket::ClosingState:

    There are more states which you do not cover



  • @jsulm Its status is not described because it is not particularly necessary.
    I try

    qDebug() << m_pTcpSocket->state().
    

    in fnTcpStateChanged().but, console don't show debug string


Log in to reply