Solved QT - QTcpSocket read and write data continuously, Client always receives empty string
-
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;
}
-
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
-
@aha_1980 Thanks for your answer. I have to do this thread because I want to send continuous data without blocking main program.
-
@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.
-
-
Hi @Ayse
Qt uses Signals&Slots for that. Effectively, writing and reading sockets is non-blocking. Just look at the example I gave you.
-
-
@Ayse You're welcome :)