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

QTcpSocket not Receiving Data Sent from Server



  • I'm having trouble sending data from a server to a client using Qt. The client is not receiving the data I'm sending from the server, which is a test string of Some random data (I've tried with and without the newline character), but I'm not even sure if the data is being sent to the client, because after doing client->write(), I get an Unknown error when doing client->errorString(), and the bytesWritten signal is never emitted.

    The writeData method is what is being called in the MainWindow class, but to try and narrow down the cause of the problem, I moved the writing of data to the client into the newConnection method.

    The message Connection received is printed to the output window. I'm sending the string Some random data in the newConnection method to the client for testing purposes, but this is not being received by the client (the code to output the received data on the client side is inside the Character::readData() method).

    client->errorString() gives Unknown error, and then the message Bytes written is printed (even though, evidently, nothing is written, but I'm just using it as a status message).

    Server.cpp

    #include "Server.h"
    
    Server::Server(QObject *parent) : QObject(parent)
    {
        server = new QTcpServer(this);
    
        qDebug() << connect(server, SIGNAL(newConnection()), SLOT(newConnection()));
        qDebug() << connect(server, SIGNAL(bytesWritten()), SLOT(bytesWritten()));
    
        qDebug() << "Listening:" << server->listen(QHostAddress::Any, 1451);
    
        server->waitForNewConnection(-1);
    }
    
    void Server::newConnection()
    {
        qDebug("Connection received");
    
        client = server->nextPendingConnection();
    
        client->write("Some random data\n");
    
        qDebug() << "Error: " << client->errorString();
    
        qDebug() << "Bytes written";
    }
    
    void Server::bytesWritten(qint64 bytes)
    {
        qDebug() << "Bytes written: " << QString::number(bytes);
    }
    
    void Server::writeData(std::string data)
    {
        QByteArray byteArray = QByteArray(data.c_str());
    
        qDebug() << "Write data: " << QString::fromStdString(data);
        client->write(byteArray);
    }
    

    Client.cpp

    #include "Client.h"
    #include "mainwindow.h"
    
    Client::Client(QObject* parent) : QObject(parent)
    {
        socket = new QTcpSocket(this);
    
        (void)QObject::connect(socket, SIGNAL(connected()), this, SLOT(connected()));
        qDebug() << "Connect signal" << QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    }
    
    bool Client::connectToHost(QString host)
    {
        socket->connectToHost(host, 1451);
        socket->waitForConnected();
    
        qDebug() << "Error: " << QString::number(socket->error() == QAbstractSocket::UnknownSocketError);
    
        return true;
    }
    
    void Client::connected()
    {
        qDebug("Socket is connected");
    
        qDebug() << QString::number(socket->state() == QAbstractSocket::ConnectedState);
    }
    
    void Client::readData()
    {
        qDebug("Read data");
    
        QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
        QByteArray data = sender->readAll();
    
        std::string character = data.toStdString();
    
        qDebug() << "Character received: " << QString::fromStdString(character);
    
        MainWindow::characterReceived(character);
    }
    

    The weird thing is that the server says Connection received, but then says Error: Unknown error on my debug line (line 28 of Server.cpp). And the client is in the ConnectedState according to the debug line (line 26 of Client.cpp prints out 1, or true), but it also gives UnknownSocketError (line 17 of Client.cpp).



  • @Bob-Jones
    In a word, I think that you should not be looking at any socket->error() unless the operation returned that an error has occurred. Otherwise, you may just get "unknown error". I can't follow from what you say, but does the code actually work regardless of error message?



  • The code gets as far as receiving a connection from the client, and then it's supposed to send data to the client from the client->write(...) method, but I'm not sure if the problem is that the data is not being written, or if the client is just not receiving the data.

    In essence, the client is not receiving the data sent from the server, no matter what I try.

    Also, I thought that QTcpServer had a bytesWritten signal, but that was for QTcpSocket, so you can ignore that. I've remove that code, but that hasn't seemed to make a difference.

    Let me known if you need more details.



  • @Bob-Jones what if you try checking this example about how to setup a client and server in asynchronous way.


Log in to reply