I have created a TCPSocket in QT, but how do i implement received data from QTCPSocket?

317 Views Asked by At

As the title says, i have created a client which receives data from my server. So far i can print it in my console, but i would like to work with it. (The data that the client recevies is from my Smartphones accelerometer sensor)

My Socket.h class:

#ifndef SOCKETTEST_H
#define SOCKETTEST_H

#include <QObject>
#include <QDebug>
#include <QTcpSocket>
#include <QAbstractSocket>

class SocketTest : public QObject
{
    Q_OBJECT
public:
    explicit SocketTest(QObject *parent = nullptr);
    void Test();

signals:

public slots:
    void connected();
    void disconnected();
    void bytesWritten( qint64 bytes);
    void readyRead();

private:
    QTcpSocket *socket;


};

#endif // SOCKETTEST_H

and socket.cpp :

#include "sockettest.h"

SocketTest::SocketTest(QObject *parent)
    : QObject{parent}
{

}
void SocketTest::Test()
{
    socket = new QTcpSocket(this);
    connect (socket, SIGNAL(connected()),this, SLOT(connected()));
    connect (socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
    connect (socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
    connect (socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));

    qDebug() <<"Connecting...";

    socket->connectToHost("MY IP", PORT);

    if(!socket->waitForConnected(2000))
    {
        qDebug() <<"Error" << socket->errorString();
    }
}

void SocketTest::connected()
{

        qDebug() <<"Connected!";

        socket->write("\r\n\r\n\r\n\r\n");


}

void SocketTest::disconnected()
{
    qDebug() <<"Disconnected!";

}

void SocketTest::bytesWritten(qint64 bytes)
{
    qDebug() <<"We wrote: " <<bytes;


}


void SocketTest::readyRead()
{
    qDebug() << socket->readAll();

}

typically what i want to create, in another class, is an if statment which moves an Object based on the sent data :

   if( received_data == 5 ){

     this->setX(this->x()+1);

} 
0

There are 0 best solutions below