problem with QLocalSocket sending continues data to QLocalServer

I'm trying to send some data from QLocalSocket to QLocalSever in a loop. Sever only gets the first data and not receiving subsequent data, but if I introduce 1 mec delay between each call from the client then the server starts to receive everything. Please check out the below Client & Server code.


#include "client.h"
#include "QDataStream"
#include <QTest>

TestClient::TestClient() : m_socket{new QLocalSocket(this)}{
    if (m_socket->waitForConnected(1000)) {
      qDebug("socket Connected!");
    connect(m_socket, &QLocalSocket::readyRead, this, &TestClient::onNewData);

void TestClient::onNewData() {
    qCritical() << "data received from server";

void TestClient::sendDataToServer() {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    QString testString = "test data";
    out << quint32(testString.size());
    out << testString;

void TestClient::startClient() {
    for(int i = 0; i < 5; i++) {
        //QTest::qWait(1); //works if I uncomment this line


#include "server.h"

TestServer::TestServer() : m_server{new QLocalServer(this)} {
    if (!m_server->listen("TestServer")) {
        qCritical() << "couldn't connect to server";
    connect(m_server, &QLocalServer::newConnection, this, &TestServer::onNewConnection);

void TestServer::onNewConnection() {
    m_socket = m_server->nextPendingConnection();
    connect(m_socket, &QLocalSocket::readyRead, this,
    connect(m_socket, &QLocalSocket::disconnected, m_socket,

void TestServer::onNewData() {
    QLocalSocket* client = qobject_cast<QLocalSocket*>(sender());
    qCritical() << "data read by server";

from the qt doc, it's stated that

readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true).

so is this my problem? adding timer is the only solution here?

You can compile this test project ->

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum