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. Question about QTcpSocket::write()

Question about QTcpSocket::write()

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 548 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.
  • ODБOïO Offline
    ODБOïO Offline
    ODБOï
    wrote on last edited by ODБOï
    #1

    Hi,

    I wander if it is safe to call (in my qml (js) code in a for loop ) an object method that will call QTcpSocket::write()

    i have this in my QML :

       function exportResults(){
            var ettList = []
            ettList = entityManager.getEntityArrayByType("boxPrg") // this is FELGO : i just get a list of my components created earlier 
    
            for(var i in ettList){         
                plc.setProgramPath(i,ettList[i].pname)
                plc.setOriginPoint(i, (ettList[i].x*courseX)/maxWidth, (ettList[i].y*courseY)/maxHeight);
                plc.setRotation(i,ettList[i].rotation);
             }
        }
    

    plc is my object and methods setProgramPath, setOriginPoint, setRotation look like this

    void PA_PLC_Interface::setRotation(int index , const QString &val){
     // ... very few (quick) processing here 
        sk->write(cmd.toLatin1(),cmd.length());
    }
    

    I wonder about the timing/synchro because i call write() inside a loop. is this safe or not ?

    what i'm writing every time (sending over tcp ) is short packets, it looks like this

    "<set><var>JM2D.xOrigins[0]</var><val>555</val></set>"
    

    thanks

    aha_1980A 1 Reply Last reply
    0
    • ODБOïO ODБOï

      Hi,

      I wander if it is safe to call (in my qml (js) code in a for loop ) an object method that will call QTcpSocket::write()

      i have this in my QML :

         function exportResults(){
              var ettList = []
              ettList = entityManager.getEntityArrayByType("boxPrg") // this is FELGO : i just get a list of my components created earlier 
      
              for(var i in ettList){         
                  plc.setProgramPath(i,ettList[i].pname)
                  plc.setOriginPoint(i, (ettList[i].x*courseX)/maxWidth, (ettList[i].y*courseY)/maxHeight);
                  plc.setRotation(i,ettList[i].rotation);
               }
          }
      

      plc is my object and methods setProgramPath, setOriginPoint, setRotation look like this

      void PA_PLC_Interface::setRotation(int index , const QString &val){
       // ... very few (quick) processing here 
          sk->write(cmd.toLatin1(),cmd.length());
      }
      

      I wonder about the timing/synchro because i call write() inside a loop. is this safe or not ?

      what i'm writing every time (sending over tcp ) is short packets, it looks like this

      "<set><var>JM2D.xOrigins[0]</var><val>555</val></set>"
      

      thanks

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @LeLev,

      in principle it is safe, but you have to be aware of two things:

      1. the write data is buffered
      2. actual transmission is done when the event loop is entered

      I'm not sure it is always handled like this on all platforms, but it applies as general rule.

      So if you need your data send out immediately, calling write() in a loop might not do what you expect from it.

      Regards

      Qt has to stay free or it will die.

      ODБOïO 1 Reply Last reply
      4
      • aha_1980A aha_1980

        Hi @LeLev,

        in principle it is safe, but you have to be aware of two things:

        1. the write data is buffered
        2. actual transmission is done when the event loop is entered

        I'm not sure it is always handled like this on all platforms, but it applies as general rule.

        So if you need your data send out immediately, calling write() in a loop might not do what you expect from it.

        Regards

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @aha_1980 Thank you very much. I don't need to send data immediately so i will try implement this approach.

        JonBJ 1 Reply Last reply
        1
        • ODБOïO ODБOï

          @aha_1980 Thank you very much. I don't need to send data immediately so i will try implement this approach.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @LeLev
          Additional to @aha_1980 's:

          So if you need your data send out immediately, calling write() in a loop might not do what you expect from it.

          be aware that you can always call https://doc.qt.io/qt-5/qabstractsocket.html#flush if you do want to to send it now (without event loop, so far as I understand).

          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