Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] QTcpSocket packet buffer
vezprog last edited by
So I have a general QTcpSocket question regarding the incoming read buffer. I have set up a client server application. The server application is written in straight C++ on a linux system and the client is in Qt 4.8 on windows 7 (using the Qt Creator).
When the client connects to the server, the server immediately starts sending packets to the client every 20 mS (due to timing, this has to remain this fast). The packet size varies with respect to which state the server is in, so therefore the packet size is unknown at all times (usually will be 30 bytes but it varies whether or not I am sending a file so it could be any packet size).
On the client side in my Qt application once the socket is connected, I am looping every 20 mS in a thread reading the socket and getting the data using waitForReadyRead(), then reading the data into a QByteArray and emitting signals to the main ui from there...
The issue I am having is this:
say I am always sending 30 bytes from the server side, constantly pounding out packets every 20 mS, the Qt Socket will wait until the QTcpSocket buffer is at 90 bytes, then the ready read signal will fire. I have tested this because when I receive a ready read signal, I use a QByteArray and check the byte size and it is constantly 90 bytes which is complete throwing off my timing. This is in no way shape or form on what I want. The way I worked around this before changing the server code to send out different sized packets was to change the read buffer on the Qt side to 30 bytes. Then I was getting the correct sized data all the time and was able able to poll every 20 mS successfully and be in sync with the server. Now with different packet sizes I am unable to do this.
Maybe I have a different understanding but I thought that one of the benefits of TCP sockets were that the handshaking between the client and the server makes it so that you will rarely drop packets in where UDP there is no error checking so to speak. So if I ship 30 bytes over, I should get a 30 byte packet on the other side. If I ship a 14000 byte packet, I should get a 14000 byte packet on the other side, how ever which way the TCP layer handles it once that packet size becomes larger.
I am not sure if this is a readyReadSignal issue where there is some overhead to when the event actually gets triggered or if this is a TCP issue. I have done 10 mS packets with a ready read signal with UDP sockets and never had an issue with a linux server and a Qt client.
Is there something in the QTcpSocket configuration that I am missing or a socket option to have it wait until there is 90 bytes on a 30 byte packet or 500 bytes on a 14000 byte packet? My application is updating a qwt plot real time so I need the packets to be "all there" no matter the size and in sync with the server.
Thanks in advance
vezprog last edited by
I actually ended up figuring it out...I set the socket options TCP_NODELAY and the send timout socket option on my server side, and on the Qt side I set the LowDelayOption to 1 and all the packets are coming in quite nicely now no matter the side.