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. [SOLVED] QTcpSocket would not receiving all data
QtWS25 Last Chance

[SOLVED] QTcpSocket would not receiving all data

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.5k 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.
  • G Offline
    G Offline
    Gundi
    wrote on last edited by
    #1

    All I am working on with some of the QT Networking tools to send data between two laptops. I am having an issue where QTcpSocket does not seem to receive all the data sent. I have used wireshark and check bytes written (from server) returned from the socket-> write function. It is strange because one execution I will get all 4999 packets (all packets) and if I execute it again it will receive only 2920 packets. I have tried to implement all the tips from other threads but I am clearly missing something. Here is my code and any help is appreciated

    Server Side Writing the data

    @
    void TcpServer::writeData()
    {
    QByteArray test;

    for(int ctr = 1; ctr < 5000; ctr++)
    {
        test.append("A");
    }
    
    qDebug() << "Inside Write";
    
    QTcpSocket *socket = new QTcpSocket(this);
    socket->connectToHost(QHostAddress("156.80.124.103"), 1234);
    qint64 bytes = socket->write(test);
    socket->waitForBytesWritten();
    socket->close();
    
    qDebug() << "Bytes written = " << bytes;
    

    }
    @

    One the client side I am reading the data back in.

    @
    void myServer::newConnection()
    {
    qDebug() << "Detected Connection";
    QByteArray testing;
    QTcpSocket *socket = server->nextPendingConnection();
    qDebug() << "Wait for connect = " << socket->waitForConnected();

    if(socket->waitForReadyRead(3000))
    {
        while(socket->bytesAvailable() > 0)
        {
            testing.append(socket->readAll());
            socket->flush();
        }
    }
    else
    {
        qDebug() << "ReadyReady Timed out";
    }
    qDebug() << "Final Testing is size = " << testing.size();
    socket->deleteLater();
    

    }
    @

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      HI,

      try with

      @void myServer::newConnection()
      {
      qDebug() << "Detected Connection";
      QByteArray testing;
      QTcpSocket *socket = server->nextPendingConnection();
      qDebug() << "Wait for connect = " << socket->waitForConnected();

      while (socket->waitForReadyRead(3000))
      {
          while(socket->bytesAvailable() > 0)
          {
              testing.append(socket->readAll());
              socket->flush();
          }
      }
      qDebug() << "ReadyReady Timed out";
      qDebug() << "Final Testing is size = " << testing.size();
      socket->deleteLater();
      

      }@

      this why once you read all data from socket more data can arrive

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gundi
        wrote on last edited by
        #3

        Thank you for the response. The first time I get 4999 and the second time with the else removed I still get 2920. Very strange. the output of the debug for two executions reads as follows.

        Server Started
        Detected Connection
        Wait for connect = true
        Final Testing is size = 4999
        Detected Connection
        Wait for connect = true
        Final Testing is size = 2920

        I just don't get why it would run once but have issues the second time. No matter how many times I run it after the initial run its 2920 packets. Strange

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gundi
          wrote on last edited by
          #4

          Thank you that worked. The while loop on the waitForReady. Thank you so much!

          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