Send Custom class object using QtcpSocket and QtcpServer
-
You need to serialize your class and deserialize it on the other side. You can use QDataStream for this or write it by your own.
-
You need to serialize your class and deserialize it on the other side. You can use QDataStream for this or write it by your own.
deserialize not working. after deserialize getName() [ myserver.cpp -- readredy() ] returns empty string
my entire test project : https://github.com/Anmol-A-jain/test.git
i'm trying to send Data class over network.
data.h#ifndef DATA_H #define DATA_H #include <QtCore> class Data { public: Data(); void setName(QString name); QString getName(); private: QString Name; }; #endif // DATA_H
data.cpp
#include "data.h" Data::Data() { this->Name = "null"; } void Data::setName(QString name) { this->Name = name; } QString Data::getName() { return this->Name; }
client:
main.cpp#include <QCoreApplication> #include <QTcpSocket> #include <QDataStream> #include <QDebug> #include "data.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket* client = new QTcpSocket; client->connectToHost("127.0.0.1", 5555); Data d; d.setName("Anmol"); QByteArray byte; qDebug() << "d String :" << d.getName(); QDataStream data(&byte,QIODevice::WriteOnly); data.writeRawData(reinterpret_cast<const char*>(&d),sizeof(d)); qDebug() << byte; client->write(byte); return a.exec(); }
server :
myserver.h#ifndef MYSERVER_H #define MYSERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> class myServer : public QObject { Q_OBJECT private: QTcpServer* server; public: explicit myServer(QObject *parent = nullptr); signals: public slots: void newConnection(); void readredy(); }; #endif // MYSERVER_H
myserver.cpp
#include "myserver.h" #include "data.h" myServer::myServer(QObject *parent) : QObject(parent) { server = new QTcpServer(); clientList = new QVector<QTcpSocket*>(); connect(server,SIGNAL(newConnection()),this,SLOT(newConnection())); if(server->listen(QHostAddress::Any,5555) ) { qDebug() << "connection started" << endl; } else { qDebug() << "connection not started" << endl; } } void myServer::newConnection() { QTcpSocket *client = server->nextPendingConnection(); connect(client,&QTcpSocket::readyRead,this,&myServer::readredy ); } void myServer::readredy() { QTcpSocket *client = static_cast<QTcpSocket *>(QObject::sender()); QByteArray byte = client->readAll(); QDataStream read(&byte,QIODevice::ReadOnly); Data d1 ; qDebug() << "d1 String before deserialize :" << d1.getName(); read.readRawData(reinterpret_cast<char*>(&d1),sizeof(d1)); qDebug() << "d1 String after deserialize :" << d1.getName(); qDebug() << "Message: " + d1.getName() ; delete client; }
main.cpp
#include <QCoreApplication> #include "myserver.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); myServer s; return a.exec(); }
Error picture :
left one is Client app and right one is Server app:
-
Please take a look at the QDataStream documentation - you can't simply copy the (expected) Memory of your class - you have to (de)serialize your members of the class with the help of QDataStream.
-
deserialize not working. after deserialize getName() [ myserver.cpp -- readredy() ] returns empty string
my entire test project : https://github.com/Anmol-A-jain/test.git
i'm trying to send Data class over network.
data.h#ifndef DATA_H #define DATA_H #include <QtCore> class Data { public: Data(); void setName(QString name); QString getName(); private: QString Name; }; #endif // DATA_H
data.cpp
#include "data.h" Data::Data() { this->Name = "null"; } void Data::setName(QString name) { this->Name = name; } QString Data::getName() { return this->Name; }
client:
main.cpp#include <QCoreApplication> #include <QTcpSocket> #include <QDataStream> #include <QDebug> #include "data.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket* client = new QTcpSocket; client->connectToHost("127.0.0.1", 5555); Data d; d.setName("Anmol"); QByteArray byte; qDebug() << "d String :" << d.getName(); QDataStream data(&byte,QIODevice::WriteOnly); data.writeRawData(reinterpret_cast<const char*>(&d),sizeof(d)); qDebug() << byte; client->write(byte); return a.exec(); }
server :
myserver.h#ifndef MYSERVER_H #define MYSERVER_H #include <QObject> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> class myServer : public QObject { Q_OBJECT private: QTcpServer* server; public: explicit myServer(QObject *parent = nullptr); signals: public slots: void newConnection(); void readredy(); }; #endif // MYSERVER_H
myserver.cpp
#include "myserver.h" #include "data.h" myServer::myServer(QObject *parent) : QObject(parent) { server = new QTcpServer(); clientList = new QVector<QTcpSocket*>(); connect(server,SIGNAL(newConnection()),this,SLOT(newConnection())); if(server->listen(QHostAddress::Any,5555) ) { qDebug() << "connection started" << endl; } else { qDebug() << "connection not started" << endl; } } void myServer::newConnection() { QTcpSocket *client = server->nextPendingConnection(); connect(client,&QTcpSocket::readyRead,this,&myServer::readredy ); } void myServer::readredy() { QTcpSocket *client = static_cast<QTcpSocket *>(QObject::sender()); QByteArray byte = client->readAll(); QDataStream read(&byte,QIODevice::ReadOnly); Data d1 ; qDebug() << "d1 String before deserialize :" << d1.getName(); read.readRawData(reinterpret_cast<char*>(&d1),sizeof(d1)); qDebug() << "d1 String after deserialize :" << d1.getName(); qDebug() << "Message: " + d1.getName() ; delete client; }
main.cpp
#include <QCoreApplication> #include "myserver.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); myServer s; return a.exec(); }
Error picture :
left one is Client app and right one is Server app:
@Anmol said in Send Custom class object using QtcpSocket and QtcpServer:
deserialize not working. after deserialize getName() [ myserver.cpp -- readredy() ] returns empty string
Your code is wrong.
You've written a memory address and then expect to read that memory address on the other side? It's not going to work like that. It's a small wonder it doesn't simply crash. Don't be lazy, write a couple of functions and serialize the data properly, which incidentally means: don't usereinterpret_cast
, especially when you're not 100% sure what it does.