Qt Forum

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

    I have a problem with QTcpSocket and Connect()!

    General and Desktop
    3
    12
    2599
    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.
    • X
      xmaze last edited by

      Hi!!

      I have a thread to read a buffer and an other function connecting with signal ReadyRead to fill the buffer with data. The problem is that after 30 seconds the signal stops to working.
      This is the code! The other program send to the tcp port only 1448 bytes and i need more byte for the signal processing, so i am waiting until the buffer has more bytes, 25KB and more, and the i try to process the incoming data. I don't know why, but the TcpData() after some time stops to called from the program. Any ideas?

      Constructor {
      socket = new QTcpSocket( this ); 
      socket->connectToHost("192.168.0.124", 5000);
      socket->setReadBufferSize(256*1024);
      BUFFER = new char[256*1024];
      connect( socket, SIGNAL(readyRead()),this, SLOT(TcpData()));
      p=0;
      }
      
       void *MainWindow::readThreaddata() {
        pthread_mutex_lock(&data_mutex);
       while(1) {
      
          if (data_ready) {
         pthread_cond_wait(&data_cond,&data_mutex);
         continue;
         }
       /* Move the last part of the previous buffer, that was not processed,
       * on the start of the new buffer. */
      memcpy(data, data+DATA_LEN, (FULL_LEN-1)*4);
      /* Read the new data. */
      memcpy(data+(FULL_LEN-1)*4, BUFFER, DATA_LEN);
      memcpy(BUFFER,BUFFER+DATA_LEN,p);
      if(p>DATA_LEN) hp=p-DATA_LEN;
      data_ready = 1;
      
      pthread_cond_signal(&data_cond);
      
      pthread_mutex_unlock(&data_mutex);
      }
      }
      
      void MainWindow::TcpData()
      {  
      pthread_mutex_lock(&Modes.data_mutex);
      
      socket->read(BUFFER+p,1448);
       p=+1448;
      qDebug()<<"TCP";
       pthread_mutex_unlock(&Modes.data_mutex);
      }
      
      1 Reply Last reply Reply Quote 0
      • J
        jalomic last edited by jalomic

        @xmaze said:
        Why you are using Thread ?
        Look this example http://doc.qt.io/qt-5/qtnetwork-fortuneclient-example.html

        X 1 Reply Last reply Reply Quote 0
        • X
          xmaze @jalomic last edited by

          @jalomic

          Because is more familiar to me and QThread is difficult at this moment for me!

          J 1 Reply Last reply Reply Quote 0
          • J
            jalomic @xmaze last edited by

            @xmaze You not need to use threads at all.

            X 1 Reply Last reply Reply Quote 0
            • X
              xmaze @jalomic last edited by xmaze

              @jalomic but i have one more thread for the data processing!

              J 1 Reply Last reply Reply Quote 0
              • J
                jalomic @xmaze last edited by

                @xmaze
                Signal not emitted because all data is received by socket. You need just read it.
                socket->read(BUFFER+p,1448);
                Here you need to check bytesAvailable() > 1448 and than read

                1 Reply Last reply Reply Quote 0
                • J
                  jalomic last edited by jalomic

                  And forgot ! Of course you need to call readAll insted read. Because if new data will not send to socket - signal readyRead will never be emited ! And data will be lost

                  X 1 Reply Last reply Reply Quote 0
                  • dheerendra
                    dheerendra Qt Champions 2022 last edited by

                    You may be hitting the even processing issue as signal/slots across thread works using events. Try moving the QTCP Socket object creation to new thread. Don't create the object in constructor. Another suggestion is that you work with single thread and see whether your idea works.

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    X 1 Reply Last reply Reply Quote 4
                    • X
                      xmaze @dheerendra last edited by

                      @dheerendra can you be more clear? i cannot understand your suggestion! Thank you

                      1 Reply Last reply Reply Quote 0
                      • X
                        xmaze @jalomic last edited by

                        @jalomic But with ReadAll i cannot use pointers to manipulate the position of the buffer, so i cannot write the data and the end of the buffer ?
                        An other problem is that ReadAll needs a QbyteArray and this make the work complicated because i need the pointers and with QbyteArray is a class...

                        J 1 Reply Last reply Reply Quote 0
                        • J
                          jalomic @xmaze last edited by

                          @xmaze
                          Oh
                          QByteArray array = socket ->readAll();
                          memcpy(BUFFER+p,array.data(),array.size())
                          p+=array.size();

                          X 1 Reply Last reply Reply Quote 0
                          • X
                            xmaze @jalomic last edited by

                            @jalomic thank you

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