gpt4 book ai didi

Linux Netcat 按预期工作但在 Raspberry Pi 上不是 QTCPSocket

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:07 26 4
gpt4 key购买 nike

我有 2 个 Raspberry Pi,一个发送器和一个接收器,它们用作使用 USB WiFi 加密狗的接入点。我在发送器上有 Qt 5.4.0 代码,它使用 USB/FTDI XBee SB6 WiFi 单元在作为客户端成功连接到它的接入点后向接收器 Pi 发送 TCP 数据包。

代码通过 XBee 将 TCP 数据包正确发送到接收器 Pi,因为我可以在接收器上使用 Netcat 程序并观察数据包成功到达端口 0x2616 ( 9750 ):

>> sudo nc -l 10.10.10.1 9750
>> HELLOHELLOHELLO

当我尝试使用 QTCPSocket 将接收器 Pi 上的 Netcat 替换为以下 Qt 代码时,它从未在套接字上接收到任何数据。我的意思是永远不会调用“readyRead()”槽。我以 sudo 运行它,发送方 Pi 正在执行与 Netcat 捕获输出时完全相同的传输。到底是怎么回事?我是否将 QTCPSocket 连接到本地端口错误?我怎样才能让它发挥作用?谢谢!

#include "tcpreceiver.h"

// Debug
#include <QDebug>
#define API_DEBUG true
#include <QApplication>

TcpReceiver::TcpReceiver(QObject *parent) :
QObject(parent)
{

// Debug
qDebug() << "Setting up a TCP Socket...";

// Create a socket
m_Socket = new QTcpSocket(this);

// Bind to the 2616 port
m_Socket->connectToHost("10.10.10.1", 0x2616);
//m_Socket->connectToHost( QHostAddress::Any, 0x2616 );

qDebug() << "Socket is valid: " << m_Socket->isValid();
//qDebug() << "Socket value: " << m_Socket->

// Get notified that data is incoming to the socket
connect(m_Socket, SIGNAL(readyRead()), this, SLOT(readyRead()));

// Init to Zero
m_NumberTCPPacketsReceived = 0;

}

void TcpReceiver::readyRead() {

qDebug() << "Received data...";

// When data comes in
QByteArray buffer;
buffer.resize(m_Socket->bytesAvailable());

// Cap buffer size
int lenToRead = buffer.size();
if ( buffer.size() > NOMINAL_AUDIO_BUFFER_SIZE ) {
lenToRead = NOMINAL_AUDIO_BUFFER_SIZE;
}

// Read the data from the TCP Port
m_Socket->read(buffer.data(), lenToRead);

...

// Count up
m_NumberTCPPacketsReceived++;

}

最佳答案

这是你如何做的:

#include "tcpreceiver.h"

// Debug
#include <QDebug>
#include <QHostAddress>

TcpReceiver::TcpReceiver(QObject *parent) :
QObject(parent)
{

// Create a server
qDebug() << "Creating a TCP Server...";

// Create the server
m_Server = new QTcpServer(this);

// Listen on the proper port
m_Server->listen( QHostAddress::Any, 0x2616 );

// Hook up signal and slots
connect(m_Server, SIGNAL(newConnection()), this, SLOT(gotNewConnection()));
connect(m_Server, SIGNAL(acceptError(QAbstractSocket::SocketError)), this, SLOT(error()));

}

void TcpReceiver::gotNewConnection() {

qDebug() << "Got a new TCP Connection";

// Get the socket
m_Socket = m_Server->nextPendingConnection();
if(m_Socket->state() == QTcpSocket::ConnectedState)
{
qDebug() << "Socket was connected at: " << m_Socket->peerAddress();
}

// Hook up some signals / slots
connect(m_Socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(m_Socket, SIGNAL(readyRead()),this, SLOT(readyRead()));

}

void TcpReceiver::disconnected() {

qDebug() << "Socket Disconnected...";

// Cleanup
m_Socket->deleteLater();

}

void TcpReceiver::error() {

qDebug() << "Error: " << m_Server->errorString();

}

void TcpReceiver::readyRead() {

qDebug() << "Received data...";

// Now read data
QByteArray buffer;
if (m_Socket->canReadLine()) {

buffer = m_Socket->readLine();
qDebug() << "Got Data: " << buffer;
}

}

关于Linux Netcat 按预期工作但在 Raspberry Pi 上不是 QTCPSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28457035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com