简体   繁体   中英

Qt TCP server/multi-client message reading

I am working on a client/server application (using qt for tcp). The clients have to send about 15 messages per second to the server. The problem is this:
the messages from the clients are received in groups. What i mean: when i get the readyRead() signal and i read the data from the socket, there are multiple messages in the buffer.
This of-course causes lag in the system.

I tried putting the incoming connections in separate threads (thread per connection) but there was no improvement.
I also tried to rise a thread each time i got a readyRead() signal, but again nothing...

BUT when i run a number of clients on the same pc as the server, everything seems ok. When using different pc's over the network, the lag occurs... (the network used is 100Mbps LAN, the messages are <200KB, and ping between pc's is <5msec, so i don't believe it's a network issue)

On the client side, the code to write the data is pretty simple:

tcpSocket->write(message.toUtf8());  
tcpSocket->waitForBytesWritten();  
tcpSocket->flush();

I also tried it without flush() or waitForBytesWritten() but the same...

EDIT: Using Qt 4.8.4 and Windows 7 and XP

Anybody has any idea how to overcome this?
Thank you in advance!

The last time I ran into a similar problem was with the stdin/stdout communication of a QProcess of Qt3.3. It behaved completely different on Linux and Windows.

Finally we found out that on Linux it used select() to react asynchronously when data arrived (fast, in most cases only one line readable) while on Windows the existence of new data was polled via a QTimer from the Qt mainloop (large delay, several messages available). A workaround we tried was to reduce the timer period in the source of Qt, but at the end we switched to shared memory based on the native OS mechanisms.

Your description sounds like you are using a similar Qt version on a Windows OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM