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

QTcpSocket::disconnected() can not be emitted in some cases



  • Hi guys,
    I find QTcpSocket::disconnected cant be emitted in some cases in my code.
    When I use software to connect my server and disconnect it, QTcpSocket::disconnected can be emitted.
    When I use my 2 network hardware to connect the server,
    One of them cant make the QTcpSocket::disconnected work when I shut down the hardware,
    Another one can make the QTcpSocket::disconnected work when I shut down, but it works after about 10 seconds
    It's very strange and confused me.

    I simplify my code as follows:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<qfile.h>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        server = new QTcpServer();
        connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect);
    
        int port = 5007;
        if(!server->listen(QHostAddress::Any,port))
        {
            ui->label_listen->setText("Cant listen");
            qDebug()<<server->errorString();
            return;
        }
    
    }
    
    MainWindow::~MainWindow()
    {
        server->close();
        server->deleteLater();
        delete ui;
    }
    
    void MainWindow::server_New_Connect()
    {
        socket=server->nextPendingConnection();
        QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
        //siganal cant be emitted here
        QObject::connect(socket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);
        ui->pushButton_send->setEnabled(true);
        ui->label->setText("new Connection");
        qDebug() << "new Connection";
    }
    
    void MainWindow::socket_Read_Data()
    {
    
    //ignore this function
    
    }
    
    void MainWindow::socket_Disconnected()
    {
      //will not be called
    }
    
    

    to see the connection state of the working socket, I change my code as following and something more strange happened.
    my 2 device can make the QTcpSocket::disconnected emitted , after I shut down the hardware.
    that is so strange, can anybody help me to understand it?
    the changed code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include<qfile.h>
    #include <QTimer>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
    
        server = new QTcpServer();
        connect(server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect);
    
        int port = 5007;
        if(!server->listen(QHostAddress::Any,port))
        {
            ui->label_listen->setText("Cant listen");
            qDebug()<<server->errorString();
            return;
        }
    
        ui->label_listen->setText("Listening");
    
    
    }
    
    MainWindow::~MainWindow()
    {
        server->close();
        server->deleteLater();
        delete ui;
    }
    
    
    void MainWindow::server_New_Connect()
    {
        socket=server->nextPendingConnection();
        QObject::connect(socket,&QTcpSocket::readyRead,this,&MainWindow::socket_Read_Data);
        QObject::connect(socket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);
        ui->pushButton_send->setEnabled(true);
        ui->label->setText("new Connection");
        qDebug() << "new Connection";
        // I set a timer to print socket state every one second.
        QTimer *timer = new QTimer(this);
        connect(timer, &QTimer::timeout, this, &MainWindow::Psocket);
        timer->setInterval(1000);
        timer->start(1000);
    }
    
    void MainWindow::socket_Read_Data()
    {
    //ignore
    }
    
    void MainWindow::socket_Disconnected()
    {
    //can be called
    }
    
    void MainWindow::Psocket()
    {
        //I think the write("test") do something because when I delete the write, it does not work again
        qDebug()<<socket->state();
        socket->write("test");
        socket->flush();
    }
    
    

    tks.



  • @gaosiy
    You are creating a QTimer everytime that your server accepts a connection, but, where are you stopping it ? Note, if your server has accepted 2 connections, it means that you have 2 QTimer running in a infinite loop.

    The disconnect signal is only emmited when the socket reaches the Unconnected State, but, it need to pass by Closing State. The socket will try to write all remaining data before of state changing. Do you remember of QTimer ? you are trying to write data to the socket buffer and in same time is waiting for writable buffer ends.



  • tks,
    this code is simple test the disconnected signal, so I just connect one client to the server and will not connect another one.

    BTW, my question is, why the hardware shutdown cant emit the disconnected signal
    (in original situation, I had not added Qtimer, add Qtimer aimed to show the sokcet state)

    about data, the server only write to the client when client send data to the server, and if I shut down the hardware device, I think no more data send to the server, and the server will not write anything as well.

    my purpose is I can check the socket disconnected and do something


Log in to reply