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. QTcpSocket transaction not working properly
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket transaction not working properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 428 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.
  • M Offline
    M Offline
    maydin
    wrote on last edited by
    #1

    I send data from my Android application written in Qt. I simply send messages from server (Android app) to client (desktop app) using this: client->write(data);

    My slot connected to ReadyRead was like this:

    void Client::readResponse() {
      auto response = tcpSocket->readAll();
      emit incomingMessage(response);
    }
    

    I saw from debugger that data comes in 2 different reads which causes incomingMessage signal emitted twice. Therefore I decided to use transactions and changed code to this:

    void Client::readResponse() {
      in.startTransaction();
    
      QByteArray response;
      in >> response;
    
      if (!in.commitTransaction())
        return;
    
      incomingMessage(response);
    }
    

    But now, I cannot even read short messages. It was working with old code if message is short but It just doesn't work now. What do I do wrong?

    Threads I found related to this:
    https://forum.qt.io/topic/83969/how-to-handle-socket-transactions-uncommitted
    https://forum.qt.io/topic/103794/qudpsocket-and-qdatastream
    https://stackoverflow.com/questions/47740923/qtcpsocket-data-arrives-late

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      When you want to use QDataStream on the reader side you also have to use it on the sender side.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Maybe this example might be helpful: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved