Qt TCP服务器不从客户端读取数据

时间:2019-09-27 19:08:36

标签: c++ qt tcp server raspberry-pi

我使用Ubuntu 16.04和QtCreator。我编写了一个从Raspberry Pi Zero W接收数据的服务器。这是PC上的服务器,Raspberry上的客户端。但是我的服务器无法读取数据。为什么?我的代码有错误吗?

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

1 个答案:

答案 0 :(得分:0)

在tcpserver.cpp中

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

在newConnection();

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