I have a problem with QTcpSocket and Connect()!



  • 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);
    }


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



  • @jalomic

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



  • @xmaze You not need to use threads at all.



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



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



  • 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



  • 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 can you be more clear? i cannot understand your suggestion! Thank you



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



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



  • @jalomic thank you


Log in to reply
 

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