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. I have a problem with QTcpSocket and Connect()!
QtWS25 Last Chance

I have a problem with QTcpSocket and Connect()!

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 2.9k Views
  • 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
    #1

    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
    0
    • J Offline
      J Offline
      jalomic
      wrote on last edited by jalomic
      #2

      @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
      0
      • J jalomic

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

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

        @jalomic

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

        J 1 Reply Last reply
        0
        • X xmaze

          @jalomic

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

          J Offline
          J Offline
          jalomic
          wrote on last edited by
          #4

          @xmaze You not need to use threads at all.

          X 1 Reply Last reply
          0
          • J jalomic

            @xmaze You not need to use threads at all.

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

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

            J 1 Reply Last reply
            0
            • X xmaze

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

              J Offline
              J Offline
              jalomic
              wrote on last edited by
              #6

              @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
              0
              • J Offline
                J Offline
                jalomic
                wrote on last edited by jalomic
                #7

                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
                0
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  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
                  4
                  • dheerendraD dheerendra

                    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.

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

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

                    1 Reply Last reply
                    0
                    • J 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 Offline
                      X Offline
                      xmaze
                      wrote on last edited by
                      #10

                      @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
                      0
                      • X xmaze

                        @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 Offline
                        J Offline
                        jalomic
                        wrote on last edited by
                        #11

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

                        X 1 Reply Last reply
                        0
                        • J jalomic

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

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

                          @jalomic thank you

                          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