[英]Qt TCP server doesn't read data from a client
我使用 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;
}
在 tcpserver.cpp
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
在 newConnection();
connect(socket, SIGNAL(readyRead()),
this, SLOT(readyRead()));
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.