How to fill a buffer in a Thread with QtTcpSocket?



  • 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


  • Lifetime Qt Champion

    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



  • @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 ?



  • QTcpSocket *a=new QTcpSocket(this);
    

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

    [edit: corrected coding tags SGaist]


Log in to reply
 

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