PDA

View Full Version : QT - QTcpSocket read and write data continuously



Ayse
30th July 2018, 12:40
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();
..
}

Ayse
31st July 2018, 06:28
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()
{
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;
}*/
}