Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved A connect function slow down the connection between QTCPserver and client

    General and Desktop
    qtcpserver qtcpsocket
    2
    4
    709
    Loading More Posts
    • 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.
    • Dooham
      Dooham last edited by

      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 1 Reply Last reply Reply Quote 0
      • Dooham
        Dooham @Dooham last edited by

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

        jsulm 1 Reply Last reply Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @Dooham last edited by

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          Dooham 1 Reply Last reply Reply Quote 6
          • Dooham
            Dooham @jsulm last edited by

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

            1 Reply Last reply Reply Quote 1
            • First post
              Last post