我使用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;
}
答案 0 :(得分:0)
在tcpserver.cpp中
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
在newConnection();
connect(socket, SIGNAL(readyRead()),
this, SLOT(readyRead()));