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. QT - QTcpSocket read and write data continuously, Client always receives empty string
Forum Updated to NodeBB v4.3 + New Features

QT - QTcpSocket read and write data continuously, Client always receives empty string

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 7.3k 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.
  • A Offline
    A Offline
    Ayse
    wrote on last edited by Ayse
    #1

    I'm using QT 4.8.6 and I'm beginner level at QT. I look at the Fortune Client-Server Example. In this example , data is sent with button click action. But I want to send data continuously in Server Side and read this continuous data in Client Side. How can I implement this code in QT ?

    I implemented this code block for continuously data flow , but I can't send data between client and server. When I was sending data from server, client got this data "", always receives empty string .

    In Server Side :

    class ProducerThread : public QThread
    {
    public :
    int socketDescriptor;
    ProducerThread(int socketDescriptor,  QObject *parent)
        : QThread(parent), socketDescriptor(socketDescriptor)
    {
    
    }
    void run()
    {
        QTcpSocket tcpSocket;
        if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
            //emit error(tcpSocket.error());
            return;
        }
        while(true)
        {
            QString deneme = " test ";
            QByteArray block;
            QDataStream out(&block, QIODevice::WriteOnly);
            out.setVersion(QDataStream::Qt_4_0);            
            out << (quint16)0;
            out <<deneme;
            out.device()->seek(0);
            out << (quint16)(block.size() - sizeof(quint16));         
            tcpSocket.write(block);
            tcpSocket.flush();//writes as much as possible            
        }
    }};
    class ProducerServer :public QTcpServer
    {
    private:
    public:
    ProducerServer(QObject *parent=0)
        : QTcpServer(parent)
    {
    
    }
    protected:
    void incomingConnection(int socketDesc)
    {
        ProducerThread *thread = new ProducerThread(socketDesc, this);
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
        thread->start();        
    }
    };
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    
    ProducerServer server;
    if (!server.listen(QHostAddress::LocalHost,12345)) {
        qDebug()<<"Threaded  Server"<<
              "Unable to start the server:";
    
    }
    return a.exec();
    }
    

    In Client Side :

    ConsumerThread.h

    #ifndef CONSUMERTHREAD_H
    #define CONSUMERTHREAD_H
    
    #include <QObject>
    #include <QtNetwork>
    
    class ConsumerThread : public QThread
    {
        Q_OBJECT
    public:
        explicit ConsumerThread(QObject *parent = 0);
        void run();
        QTcpSocket *tcpSocket;
        int blockSize;
    
    signals:
    
    public slots:
    void mySocketRead();
    
    };
    
    #endif // CONSUMERTHREAD_H
    

    ConsumerThread.cpp

    #include "consumerthread.h"
    
    ConsumerThread::ConsumerThread(QObject *parent) :
        QThread(parent)
    {
        blockSize = 0;
        tcpSocket = new QTcpSocket;
        tcpSocket->connectToHost(QHostAddress::LocalHost,12345);
    }
    void ConsumerThread::run()
    {
        connect(tcpSocket,SIGNAL(readyRead()), this, SLOT( mySocketRead()));
    
    }
    void ConsumerThread::mySocketRead(){
        qDebug() <<"here C1 bytes = " << tcpSocket->bytesAvailable();
        blockSize = 0;
        QDataStream in(tcpSocket);
        in.setVersion(QDataStream::Qt_4_0);
    
        qDebug() << "Bytes available : " << tcpSocket->bytesAvailable();
    
        QString nextFortune;
        in >>nextFortune;
        qDebug() << nextFortune;
    

    }

    aha_1980A 1 Reply Last reply
    0
    • A Ayse

      I'm using QT 4.8.6 and I'm beginner level at QT. I look at the Fortune Client-Server Example. In this example , data is sent with button click action. But I want to send data continuously in Server Side and read this continuous data in Client Side. How can I implement this code in QT ?

      I implemented this code block for continuously data flow , but I can't send data between client and server. When I was sending data from server, client got this data "", always receives empty string .

      In Server Side :

      class ProducerThread : public QThread
      {
      public :
      int socketDescriptor;
      ProducerThread(int socketDescriptor,  QObject *parent)
          : QThread(parent), socketDescriptor(socketDescriptor)
      {
      
      }
      void run()
      {
          QTcpSocket tcpSocket;
          if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
              //emit error(tcpSocket.error());
              return;
          }
          while(true)
          {
              QString deneme = " test ";
              QByteArray block;
              QDataStream out(&block, QIODevice::WriteOnly);
              out.setVersion(QDataStream::Qt_4_0);            
              out << (quint16)0;
              out <<deneme;
              out.device()->seek(0);
              out << (quint16)(block.size() - sizeof(quint16));         
              tcpSocket.write(block);
              tcpSocket.flush();//writes as much as possible            
          }
      }};
      class ProducerServer :public QTcpServer
      {
      private:
      public:
      ProducerServer(QObject *parent=0)
          : QTcpServer(parent)
      {
      
      }
      protected:
      void incomingConnection(int socketDesc)
      {
          ProducerThread *thread = new ProducerThread(socketDesc, this);
          connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
          thread->start();        
      }
      };
      int main(int argc, char *argv[])
      {
      QCoreApplication a(argc, argv);
      
      ProducerServer server;
      if (!server.listen(QHostAddress::LocalHost,12345)) {
          qDebug()<<"Threaded  Server"<<
                "Unable to start the server:";
      
      }
      return a.exec();
      }
      

      In Client Side :

      ConsumerThread.h

      #ifndef CONSUMERTHREAD_H
      #define CONSUMERTHREAD_H
      
      #include <QObject>
      #include <QtNetwork>
      
      class ConsumerThread : public QThread
      {
          Q_OBJECT
      public:
          explicit ConsumerThread(QObject *parent = 0);
          void run();
          QTcpSocket *tcpSocket;
          int blockSize;
      
      signals:
      
      public slots:
      void mySocketRead();
      
      };
      
      #endif // CONSUMERTHREAD_H
      

      ConsumerThread.cpp

      #include "consumerthread.h"
      
      ConsumerThread::ConsumerThread(QObject *parent) :
          QThread(parent)
      {
          blockSize = 0;
          tcpSocket = new QTcpSocket;
          tcpSocket->connectToHost(QHostAddress::LocalHost,12345);
      }
      void ConsumerThread::run()
      {
          connect(tcpSocket,SIGNAL(readyRead()), this, SLOT( mySocketRead()));
      
      }
      void ConsumerThread::mySocketRead(){
          qDebug() <<"here C1 bytes = " << tcpSocket->bytesAvailable();
          blockSize = 0;
          QDataStream in(tcpSocket);
          in.setVersion(QDataStream::Qt_4_0);
      
          qDebug() << "Bytes available : " << tcpSocket->bytesAvailable();
      
          QString nextFortune;
          in >>nextFortune;
          qDebug() << nextFortune;
      

      }

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Ayse, welcome to devnet!

      But I want to send data continuously in Server Side and read this continuous data in Client Side. How can I implement this code in QT ?

      Then better have a look at the loopback example

      Any reason you have to do this in a thread? After all, it makes things more difficult for starters.

      Regards

      Qt has to stay free or it will die.

      A 1 Reply Last reply
      2
      • aha_1980A aha_1980

        Hi @Ayse, welcome to devnet!

        But I want to send data continuously in Server Side and read this continuous data in Client Side. How can I implement this code in QT ?

        Then better have a look at the loopback example

        Any reason you have to do this in a thread? After all, it makes things more difficult for starters.

        Regards

        A Offline
        A Offline
        Ayse
        wrote on last edited by Ayse
        #3

        @aha_1980 Thanks for your answer. I have to do this thread because I want to send continuous data without blocking main program.

        aha_1980A 1 Reply Last reply
        0
        • A Ayse

          @aha_1980 Thanks for your answer. I have to do this thread because I want to send continuous data without blocking main program.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Ayse said in QT - QTcpSocket read and write data continuously, Client always receives empty string:

          I have to do this thread because I want to send continuous data without blocking main program.

          No, you don't need threads for that.

          Qt has to stay free or it will die.

          A 1 Reply Last reply
          1
          • aha_1980A aha_1980

            @Ayse said in QT - QTcpSocket read and write data continuously, Client always receives empty string:

            I have to do this thread because I want to send continuous data without blocking main program.

            No, you don't need threads for that.

            A Offline
            A Offline
            Ayse
            wrote on last edited by
            #5

            @aha_1980 @aha_1980 Is it possible listen and write operations at the same time ? In Java, I have to use threads for this operations and now I try it in QT C++.

            1 Reply Last reply
            0
            • aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi @Ayse

              Qt uses Signals&Slots for that. Effectively, writing and reading sockets is non-blocking. Just look at the example I gave you.

              Qt has to stay free or it will die.

              A 1 Reply Last reply
              5
              • aha_1980A aha_1980

                Hi @Ayse

                Qt uses Signals&Slots for that. Effectively, writing and reading sockets is non-blocking. Just look at the example I gave you.

                A Offline
                A Offline
                Ayse
                wrote on last edited by
                #7

                @aha_1980 @aha_1980 @aha_1980 Thank you all for your replies.

                Regards..

                aha_1980A 1 Reply Last reply
                1
                • A Ayse

                  @aha_1980 @aha_1980 @aha_1980 Thank you all for your replies.

                  Regards..

                  aha_1980A Offline
                  aha_1980A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Ayse You're welcome :)

                  Qt has to stay free or it will die.

                  1 Reply Last reply
                  0

                  • Login

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