Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Question about QTcpSocket::write()



  • 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


  • Lifetime Qt Champion

    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



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



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


Log in to reply