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

tcp socket error signal



  • Hi,

    I'm trying to handle error(QAbstractSocket::SocketError socketError) signal

        sk = new QTcpSocket(this);
    
        connect(sk, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
            [=](QAbstractSocket::SocketError err){
                    qDebug()<<"SocketError";
                });
    

    for example : In my Tcp client app if i call write() before connectToHost() QtCreator application output will say
    QIODevice::write (QTcpSocket): device not open

    but my lambda is neever called,; please tell me what i'm doing wrong



  • You are doing nothing wrong. That's just a qWarning() debug message, not an error reported by the socket


  • Qt Champions 2017

    Treat warnings you get from Qt as user-code errors. To that end, writing to a socket that's not opened is definitely a dubious choice. Write to the socket after it's valid, if you need, check with QTcpSocket::isValid before you commit to writing.



  • Thank you for inputs

    @kshegunov said in tcp socket error signal:

    To that end, writing to a socket that's not opened is definitely a dubious choice.

    i try to trigger a socket error so QAbstractSocket::error signal is emitted and i can check if my handler is called.


  • Qt Champions 2017

    @LeLev said in tcp socket error signal:

    i try to trigger a socket error so QAbstractSocket::error signal is emitted and i can check if my handler is called.

    This is not a socket error, @VRonin said it; it can't be as you have no socket (yet). This is a programming error deal with it accordingly. Think of it as if you were to trip an assert somewhere.


Log in to reply