Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to fill a buffer in a Thread with QtTcpSocket?
Qt 6.11 is out! See what's new in the release blog

How to fill a buffer in a Thread with QtTcpSocket?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 2.1k Views 2 Watching
  • 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 Offline
    X Offline
    xmaze
    wrote on last edited by xmaze
    #1

    Hi, i have a problem in a thread. I want to process some data from RTLSDR device and i use the rtl tcp program to read the raw data from a tcp connection. The problem is that this program sends 1448 bytes and i cannot change the buffer.
    I think that if i can read the socket 10 times then i can fill a buffer 10 times and then i want to process the 14480 bytes. I am not expert and i cannot find a solution. Any ideas?

    char *datas= new char[1448*10];
    QTcpSocket *a=new QTcpSocket(this);
    a->setReadBufferSize(1448);
     a->connectToHost("192.168.0.124", 5000);
    if( a->waitForConnected(1000) ) {
    }
    pthread_mutex_lock(&data_mutex);
    
    while(1) {
    while((a->waitForReadyRead(20))) {}
    a->read(datas,1448);
    /* Move the last part of the previous buffer, that was not processed,
     * on the start of the new buffer. */
    memcpy(Modes.data, Modes.data+DATA_LEN, (FULL_LEN-1)*4);
    /* Read the new data. */
    memcpy(Modes.data+(FULL_LEN-1)*4, datas,DATA_LEN);
    Modes.data_ready = 1;
    pthread_cond_signal(&data_cond);
    pthread_mutex_unlock(&data_mutex);
    

    }

    I thought with a for loop will be ok, like this :

     for(int k=0; k<10; k++) {
     while((a->waitForReadyRead(20))) {}
    a->read(datas+(k*1448),1448);
    } 
    

    but the while((a->waitForReadyRead(20))) {} crashes

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Why not use signals and slots with your socket ? Just cumulate the data read in a QByteArray and when you have enough start parsing it

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      X 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Why not use signals and slots with your socket ? Just cumulate the data read in a QByteArray and when you have enough start parsing it

        X Offline
        X Offline
        xmaze
        wrote on last edited by
        #3

        @SGaist said:

        Hi,

        Why not use signals and slots with your socket ? Just cumulate the data read in a QByteArray and when you have enough start parsing it

        And how can i send a signal to the other thread that the data is ready ?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis_
          wrote on last edited by SGaist
          #4
          QTcpSocket *a=new QTcpSocket(this);
          

          "this" is wrong. please read about QThread and related documentation..

          [edit: corrected coding tags SGaist]

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved