questions about qtcpserver and socket in Loopback Example



  • Loopback Example tell me how to use qtcpsever and qtcpsocket. but i have a question in this example .

    1. in start function:
     while (!tcpServer.isListening() && !tcpServer.listen()) {
            QMessageBox::StandardButton ret = QMessageBox::critical(this,
                                            tr("Loopback"),
                                            tr("Unable to start the test: %1.")
                                            .arg(tcpServer.errorString()),
                                            QMessageBox::Retry
                                            | QMessageBox::Cancel);
            if (ret == QMessageBox::Cancel)
                return;
        }
    

    this while function doesn't block the program ? can just write like this ?

    tcpServer.listen() ;
    tcpServer.isListening();
    
    1. in acceptConnection function :
     tcpServerConnection = tcpServer.nextPendingConnection();
        connect(tcpServerConnection, SIGNAL(readyRead()),
                this, SLOT(updateServerProgress()));
        connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
                this, SLOT(displayError(QAbstractSocket::SocketError)));
    
        serverStatusLabel->setText(tr("Accepted connection"));
        tcpServer.close();
    

    after connect operation , tcpserver just be closed , what if the transfer not finished ? why the documentary write like this ?


  • Lifetime Qt Champion

    Hi,

    1. QMessageBox::critical will spin an event loop like QDialog::exec() does hence the application continues to run.
    2. The server is closed but the connection is still opened and the transfer is done between the two QTcpSocket.


  • @SGaist said in questions about qtcpserver and socket in Loopback Example:

    Hi,

    1. QMessageBox::critical will spin an event loop like QDialog::exec() does hence the application continues to run.
    2. The server is closed but the connection is still opened and the transfer is done between the two QTcpSocket.

    thanks for your answer , the serve is closed means that this only accept one connection ?

    i have a feature want to achieve is like this example , my program has a backup feature , it use timer to control fixed time interval to transfer some files to same program on the other machine . so if once connection connect , i close the tcpserver , which signal i can use to let me known the transfer finished ? or just set the total size to control ?


  • Lifetime Qt Champion

    That's up to you to implement since you know what you are transferring. You can use QDataStream::startTransaction for that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.