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

[SOLVED] client can't disconnect user from server



  • From the client, how to disconnect to the server? in the code below, the pushbutton when clicked should disconnect the server with socket->close(); but it does not. is socket->close(); server side only. if so, what would be the alternative to disconnect the user from client side?

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    socket = new QTcpSocket(this);

    this->connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    this->connect(socket, SIGNAL(connected()), this, SLOT(connected()));

    socket->connectToHost(serverAddress, 4200);

    if (socket->waitForConnected(500))
    {
       ui->roomTextEdit->append("Connected to server ;-)");
       ui->connect->setText("Disconnect");
    
    } else {
       ui->roomTextEdit->append("Cannot connect to server");
       ui->connect->setText("Connect");
     }
    

    }

    void MainWindow::on_connect_clicked()
    {
    if (connected1 == 0)
    if (ui->connect->text() == "Connect"){
    login->show();
    ui->connect->setText("Disconnect");
    } else {
    if (connected1 == 1)
    socket->close();
    ui->connect->setText("Connect");
    }
    }@



  • i found what i was looking for. the disconnectFromHost() disconnects the client from the host.


Log in to reply