Results 1 to 2 of 2

Thread: QT - QTcpSocket read and write data continuously

  1. #1
    Join Date
    Jul 2018
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QT - QTcpSocket read and write data continuously

    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 send with button click action. But I want to sending data continuously in Server Side and reading this continuous data in Client Side. How can I implement this code in QT ?

    In Server Side :

    bool keepSending = true;

    while(keepSending)
    {
    ..
    writePacket();
    ..
    }

    In ClientSide :
    bool keepListening = true;

    while(keepListening )
    {
    ..
    readPacket();
    ..
    }
    Last edited by Ayse; 30th July 2018 at 13:16.

  2. #2
    Join Date
    Jul 2018
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT - QTcpSocket read and write data continuously

    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 ublic 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()
    {
    qDebug() <<"here C";
    connect(tcpSocket,SIGNAL(readyRead()), this, SLOT( mySocketRead()));
    /*while(true)
    {
    sleep(5);
    }*/
    }
    void ConsumerThread::mySocketRead(){
    qDebug() <<"here C1 bytes = " << tcpSocket->bytesAvailable();
    blockSize = 0;
    QDataStream in(tcpSocket);
    in.setVersion(QDataStream::Qt_4_0);

    /* if (blockSize == 0) {//Buradaki if de dönüyor
    if ( tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    return;
    //! [8]

    //! [10]

    in >> blockSize;
    qDebug() <<"here C2 block size : " << blockSize;
    }
    if ( tcpSocket->bytesAvailable() < blockSize)
    return;*/
    //! [10] //! [11]
    qDebug() <<"here C3";
    qDebug() << "Bytes available : " << tcpSocket->bytesAvailable();

    //tcpSocket->waitForReadyRead();
    //qDebug() << tcpSocket->readAll();
    QString nextFortune;
    in >>nextFortune;
    qDebug() << nextFortune;

    /*while(tcpSocket->canReadLine()){
    QString nextFortune = QString( tcpSocket>readLine().simplified());
    //in >> nextFortune;
    qDebug() << nextFortune;
    }*/
    }
    Last edited by Ayse; 31st July 2018 at 12:19.

Similar Threads

  1. Blocking QTcpSocket for read/write operations
    By vcernobai in forum Qt Programming
    Replies: 2
    Last Post: 7th December 2010, 13:37
  2. QTcpSocket write():: How many data sends?
    By Daxos in forum Qt Programming
    Replies: 3
    Last Post: 29th July 2010, 10:27
  3. QTcpSocket write() Trasmission Data Design
    By Daxos in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2010, 18:19
  4. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  5. QTcpSocket waiting for write to be read
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 19:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.