Solved QTcpSocket receives same packets 2 times in Client Side
-
I have written a simple Client Server Application in QT C++ . I have a Producer class which generates and send packets over TCP connection in Server Side , and Consumer class which reads packets over TCP connection in Client Side. Application output is shown in Figure 1. In Figure 1, same packet numbers received 2 times. What is the reason for this ? C++ codes are shown in Code blocks.
Regards..
Figure 1.Server Side :
producer.h
#ifndef PRODUCER_H #define PRODUCER_H #include <QObject> #include <QtNetwork> static qint32 counter = 1; class Packet { public: Packet(qint32 pN,qint32 d,qint32 pS) { this->packetNum = pN; this->data = d; this->packetSize = pS; } qint32 packetNum; qint32 data; qint32 packetSize; }; class Producer : public QObject { Q_OBJECT public: explicit Producer(QObject *parent = 0); signals: public slots: void acceptConnection(); void startTransfer(); private: QTcpServer tcpServer; QTcpSocket *tcpServerConnection; QByteArray byteArray; QString test; int blockSize; double * doubleArray; double * generatePacketDoubleArray(int ,int ,double); }; #endif // PRODUCER_H
producer.cpp
#include "producer.h" #include <iostream> #include <string.h> using namespace std; Producer::Producer(QObject *parent) : QObject(parent) { test = "You've been leading a dog's life. Stay off the furniture."; doubleArray = generatePacketDoubleArray(1,10,0.001); tcpServer.listen(QHostAddress::LocalHost,12345); if(!tcpServer.isListening() && !tcpServer.listen()) { qDebug() << "Unable to start " << tcpServer.errorString(); } connect(&tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection())); } void Producer::acceptConnection() { tcpServerConnection = tcpServer.nextPendingConnection(); qDebug() << "Connected.."; startTransfer(); } qint64 totalBytes = 0; void Producer::startTransfer() { while ((tcpServerConnection->state() == QAbstractSocket::ConnectedState) && counter <=10) { Packet packet(counter,counter*10,sizeof(counter)); QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << static_cast<qint32>(packet.packetNum) << static_cast<qint32>(packet.data) << static_cast<qint32>(packet.packetSize); tcpServerConnection->write(block); qDebug() << "Counter : " << counter << " -> " << tcpServerConnection->write(block); tcpServerConnection->waitForBytesWritten(); ++counter; } } double * Producer::generatePacketDoubleArray(int packetNum,int maxlen,double stepDouble) { double *array = new double[maxlen]; double increase = 0; for(int i=0;i<maxlen;++i) { increase += stepDouble; array[i] = stepDouble*maxlen*packetNum+increase; } return array; }
Client Side :
*consumer.h
#ifndef CONSUMER_H #define CONSUMER_H #include <QObject> #include <QtNetwork> static int counter = 0; class Packet { public: Packet(qint32 pN,qint32 d,qint32 pS) { this->packetNum = pN; this->data = d; this->packetSize = pS; } qint32 packetNum; qint32 data; qint32 packetSize; quint32 blockSize; }; class Consumer : public QObject { Q_OBJECT public: explicit Consumer(QObject *parent = 0); signals: public slots: void updateClientProgress(); private: QTcpSocket *tcpClient; QByteArray byteArray; qint32 blockSize; }; #endif // CONSUMER_H
consumer.cpp*
#include "consumer.h" Consumer::Consumer(QObject *parent) : QObject(parent) { tcpClient = new QTcpSocket; tcpClient->connectToHost(QHostAddress::LocalHost,12345); connect(tcpClient, SIGNAL(readyRead()),this,SLOT(updateClientProgress())); } void Consumer::updateClientProgress() { blockSize = 0; QDataStream in(tcpClient); in.setVersion(QDataStream::Qt_4_0); while(tcpClient->bytesAvailable() > 0 ) { qint32 packetNum = 0; qint32 data = 0; qint32 packetSize = 0; in >>packetNum >> data >> packetSize; qDebug() << "Packet Num : " <<packetNum; qDebug() << "Data : " <<data; qDebug() << "Packet Size : " <<packetSize; qDebug() << "\n------------------------------------------------------------\n"; } }
-
You are writing data twice. See the following. Look at your debug statement.
tcpServerConnection->write(block);
qDebug() << "Counter : " << counter << " -> " << tcpServerConnection->write(block); -
@dheerendra Thank you for your answer. I can not realise this simple fault and spend too much time.
Regards..