Linux Netcat在Raspberry Pi上的预期但不是QTCPSocket

时间:2015-02-11 14:40:14

标签: linux qt tcp qt5 qtcpsocket

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

代码正在通过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++;

}

1 个答案:

答案 0 :(得分:1)

以下是您的操作方法:

#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;
    }

}