A connect function slow down the connection between QTCPserver and client



  • Hello,
    I am developing a code for a connection between a server and a client. My goal, is to read the messages from a device connected to my computer by a serial port (this will be the server) and sending the messages to other program that decode them. I have no troubles with read the serial port and decode the messages. But I have to implement other function: sending a message to the device from the client, I can encode the message in a QByteArray, and sending to the device. However, to make this possible I use a connect function between a signal readyRead and a function to write the message in the serial port. When I implement this function, the connection between the client and the server takes a few seconds more that without the connect.

    This is the connect section:

     socket = server->nextPendingConnection();
          connect(socket,SIGNAL(disconnected()),this,SLOT(clientDisconnected()));
          connect(socket,SIGNAL(readyRead()), this, SLOT(on_readyRead()));
          qDebug()<<"Hola Nuevo Cliente";
    

    An this is the function on_readyRead()

    void MyServer::on_readyRead()
    {
        msgSend="";
        socket->waitForReadyRead(3000);
        msgSend=socket->readAll();
    
            qDebug()<<msgSend;
    mipuerto->writeMsg(msgSend);
    
    
    
    }
    

    The write function itself doesn't have any troubles, and I can send the message to the device, and it understand the message and execute the orders. However, it takes a few second to establish the connection between the server and the client. And when I send the message from the client to the server, it also takes a few seconds.
    Thanks you for your help.



  • @Dooham
    Ok, I have seen that the delay is due to waitforReadyRead(x), the wait a time and freeze the program during this time. I have changed the value from 3000 to 100 and now, the program works better, but I am not really conviced about this solution.


  • Qt Champions 2018

    @Dooham Why do you have this waitForReadyRead() in on_readyRead()?
    on_readyRead() is called when readyRead() signal is emitted and that happens when there is something to read.



  • @jsulm Hi, thanks for your answer and sorry for my delay, I haven't login in the forum during the weekend. You are right, I eliminated this line and I didn't get this trouble.


Log in to reply
 

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