简体   繁体   中英

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;
}

In tcpserver.cpp

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

In newConnection();

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

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