Search code examples
c++qttcpserverraspberry-pi

Qt TCP server doesn't read data from a client


I use Ubuntu 16.04 and QtCreator. I wrote a server that receives data from Raspberry Pi Zero W. That is server on PC, and client on Raspberry. But my server doesn't read data. Why? Is there an error in my code?

tcpserver.cpp

#include "tcpserver.h"

tcpServer::tcpServer(QObject *parent) : QObject(parent)
{
    server = new QTcpServer(this);

    connect(server, SIGNAL(newConnection()),
            this, SLOT(newConnection()));

    connect(server, SIGNAL(readyRead()),
            this, SLOT(readyRead()));

    if(!server->listen(QHostAddress::Any, 1234)){
        qDebug() << "Server could not start";
    }else{
        qDebug() << "Server started!";
    }
}

void tcpServer::newConnection(){
    socket = server->nextPendingConnection();
    qDebug() << "Client was connected!";

}

void tcpServer::readyRead(){
    QByteArray socketBuffer = socket->readAll();
    qDebug() << socketBuffer;
}

Solution

  • In tcpserver.cpp

    connect(server, SIGNAL(newConnection()),
            this, SLOT(newConnection()));
    

    In newConnection();

    connect(socket, SIGNAL(readyRead()),
            this, SLOT(readyRead()));