C++ Qt TCP服务器没有';无法从客户端读取数据
我使用Ubuntu 16.04和QtCreator。我写了一个从Raspberry Pi Zero W接收数据的服务器,它是PC上的服务器,Raspberry上的客户端。但我的服务器不读取数据。为什么?我的代码中有错误吗 tcpserver.cppC++ Qt TCP服务器没有';无法从客户端读取数据,c++,qt,tcp,server,raspberry-pi,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); con
#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;
}
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
#包括“tcpserver.h”
tcpServer::tcpServer(QObject*父对象):QObject(父对象)
{
服务器=新的QTcpServer(此);
连接(服务器,信号(newConnection()),
这个插槽(newConnection());
连接(服务器,信号(readyRead()),
这个,SLOT(readyRead());
如果(!server->listen(QHostAddress::Any,1234)){
tcpserver.cpp中的qDebug()
#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;
}
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
在newConnection()中
在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;
}
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
在newConnection()中
我认为您需要退一步,了解套接字和网络编程的一般知识。接收数据的套接字就是您在newConnection
函数中获得连接的套接字。“服务器”套接字是被动套接字,仅侦听传入连接,仅此而已。非常感谢您的评论。我理解QTcpServer和QTcpSocket的区别。我认为您需要后退一步,了解套接字和网络编程的一般知识。接收数据的套接字就是您在newConnection
function.“服务器”套接字是被动套接字,仅侦听传入的连接,仅此而已。非常感谢您的评论。我理解QTcpServer和QTcpSocket的区别。