0
votes

I have 2 Raspberry Pis, one sender and one receiver which acts as an Access Point using a USB WiFi dongle. I have Qt 5.4.0 code on the sender that uses a USB/FTDI XBee SB6 WiFi unit to send TCP packets to the receiver Pi after connecting to it's Access Point successfully as a client.

The code is sending TCP packets correctly through the XBee to the receiver Pi because I can use the Netcat program on the receiver and watch the packets arrive successfully on port 0x2616 ( 9750 ):

>> sudo nc -l 10.10.10.1 9750
>> HELLOHELLOHELLO

When I try to replace Netcat on the receiver Pi with the following Qt code using QTCPSocket, it never receives any data on the socket. By this I mean that the 'readyRead()' slot is never called. I've run it as sudo and the sender Pi is doing exactly the same transfer as it was when Netcat was capturing the output. What is going on? Am I connecting wrong with QTCPSocket to the local port? How can I make it work? Thanks!

#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
Does nc work without the sudo? Do you run your Qt program with sudo?sashoalm

1 Answers

1
votes

Here is how you do it:

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

}