Qt World Summit: Register Today!

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("", 5000);
    BUFFER = new char[256*1024];
    connect( socket, SIGNAL(readyRead()),this, SLOT(TcpData()));
     void *MainWindow::readThreaddata() {
     while(1) {
        if (data_ready) {
     /* 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);
    if(p>DATA_LEN) hp=p-DATA_LEN;
    data_ready = 1;
    void MainWindow::TcpData()

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

  • Qt Champions 2017

    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
    QByteArray array = socket ->readAll();

  • @jalomic thank you

Log in to reply