Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. como usar QTcpSocket con Qdatastream

como usar QTcpSocket con Qdatastream

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 274 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt_account
    wrote on last edited by
    #1

    buenas, soy nuevo en qt (c++) y e tratado de enviar una instacia de una clase (prueba) a través de una conexion Tcp (local) , me e basado en el envió de un qtextstream por un QTcpsocket y la serializacion de una clase por Qdatastream en un archivo.
    la verdad estoy atascado en esto ¿como enviar correctamente un datastream por medio de un Qtcpsocket?
    el codigo q uso es
    clase prueba

    public:
        prueba (){};
        void asignar(int,QString);
        friend inline QDataStream &operator<< (QDataStream &ds, prueba &p);
        friend inline QDataStream &operator>> (QDataStream &ds, prueba &p);
        int i ;
        QString text;
    };
    inline QDataStream &operator<< (QDataStream &ds, prueba &p)
    {
        return  ds << qint32(p.i)<<p.text;
    }
    inline QDataStream &operator>> (QDataStream &ds, prueba &p)
    {
    
        qint32 s;
        QString a;
        ds >> s >>a;
        p.asignar(s,a);
        return  ds;
    }
    

    servidor

    class server : public QTcpServer
    {
        Q_OBJECT
    public:
        explicit server(QObject *parent = nullptr);
        //void enviar(QString);
        void enviar(prueba*);
        QTcpSocket *socket;
    }
    
    //coneccion
    server::server(QObject *parent) : QTcpServer(parent)
        {
            socket=nullptr;
            connect(this,&server::newConnection,[&](){
               socket=nextPendingConnection();
            });
        
        }
    void server::enviar(prueba *p){
        QDataStream ds;
        ds.setDevice(socket);
        ds.setVersion(QDataStream::Qt_5_14);
        ds<< p;
    }
    

    cliente

        QTcpsocket *cliente= new QTcpSocket(nullptr);
        cliente->connectToHost("localhost",8800);
        connect(cliente,&QTcpSocket::readyRead,[&](){
            QDataStream ds(cliente);
            ds.setVersion(QDataStream::Qt_5_14);
            ds.setDevice(cliente);
            ds >> prueba ; 
            ui->numero_2->setText(QString::number(prueba.i));
            ui->texto_2->setText(prueba.text);
    
        });
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      Hi and welcome to devnet,

      Please use English as it is the official language of this forum.

      If you'd prefer to stay in Spanish, there's a dedicated sub-forum.

      Google translated: Hola y bienvenidos a Devnet.

      Utilice el inglés, ya que es el idioma oficial de este foro.

      Si prefiere quedarse en español, hay un sub-foro dedicado.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved