Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket::disconnected() can not be emitted in some cases

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 3.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • gaosiyG Offline
    gaosiyG Offline
    gaosiy
    wrote on last edited by
    #1

    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.

    Biomedical Engineering Ph.D. student, China

    KillerSmathK 1 Reply Last reply
    0
    • gaosiyG gaosiy

      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.

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      @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.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      4
      • gaosiyG Offline
        gaosiyG Offline
        gaosiy
        wrote on last edited by
        #3

        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

        Biomedical Engineering Ph.D. student, China

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved