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



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

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

>> sudo nc -l 10.10.10.1 9750
>> HELLOHELLOHELLO

当我试图使用QTCPSocket用下面的Qt代码替换接收器Pi上的Netcat时,它从未在套接字上接收到任何数据。我的意思是从来没有调用过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;
    }
}

相关内容

  • 没有找到相关文章

最新更新