<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QTcpSocket receives same packets 2 times in Client Side]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">Regards..</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/49e88377-b624-4138-8d6e-4d28cb3f0545.JPG" alt="0_1533553338191_image.JPG" class=" img-fluid img-markdown" /><br />
Figure 1.</p>
<p dir="auto"><strong>Server Side :</strong></p>
<p dir="auto"><em>producer.h</em></p>
<pre><code>#ifndef PRODUCER_H
#define PRODUCER_H

#include &lt;QObject&gt;
#include &lt;QtNetwork&gt;

static qint32 counter = 1;

class Packet
{
public:
    Packet(qint32 pN,qint32 d,qint32 pS)
    {
        this-&gt;packetNum = pN;
        this-&gt;data = d;
        this-&gt;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

</code></pre>
<p dir="auto"><em>producer.cpp</em></p>
<pre><code>#include "producer.h"

#include &lt;iostream&gt;
#include &lt;string.h&gt;

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() &amp;&amp; !tcpServer.listen())
    {
        qDebug() &lt;&lt; "Unable to start " &lt;&lt; tcpServer.errorString();
    }
    connect(&amp;tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
}
void Producer::acceptConnection()
{
    tcpServerConnection = tcpServer.nextPendingConnection();
    qDebug() &lt;&lt; "Connected..";
    startTransfer();
}
qint64 totalBytes = 0;
void Producer::startTransfer()
{
    while ((tcpServerConnection-&gt;state() == QAbstractSocket::ConnectedState) &amp;&amp; counter &lt;=10) {

        Packet packet(counter,counter*10,sizeof(counter));

        QByteArray block;
        QDataStream out(&amp;block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_4_0);

        out &lt;&lt; static_cast&lt;qint32&gt;(packet.packetNum)
            &lt;&lt; static_cast&lt;qint32&gt;(packet.data)
            &lt;&lt; static_cast&lt;qint32&gt;(packet.packetSize);
     

        tcpServerConnection-&gt;write(block);

        qDebug() &lt;&lt; "Counter : " &lt;&lt; counter &lt;&lt; " -&gt; " &lt;&lt; tcpServerConnection-&gt;write(block);

        tcpServerConnection-&gt;waitForBytesWritten();

        ++counter;
    }
}
double * Producer::generatePacketDoubleArray(int packetNum,int maxlen,double stepDouble)
{
    double *array = new double[maxlen];

    double increase = 0;

    for(int i=0;i&lt;maxlen;++i)
    {
        increase += stepDouble;
        array[i] = stepDouble*maxlen*packetNum+increase;
    }
    return array;
}

</code></pre>
<p dir="auto"><strong>Client Side :</strong></p>
<p dir="auto">*<em>consumer.h</em></p>
<pre><code>#ifndef CONSUMER_H
#define CONSUMER_H

#include &lt;QObject&gt;
#include &lt;QtNetwork&gt;

static int counter = 0;

class Packet
{
public:
    Packet(qint32 pN,qint32 d,qint32 pS)
    {
        this-&gt;packetNum = pN;
        this-&gt;data = d;
        this-&gt;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

</code></pre>
<p dir="auto">consumer.cpp*</p>
<pre><code>#include "consumer.h"

Consumer::Consumer(QObject *parent) :
    QObject(parent)
{
    tcpClient = new QTcpSocket;
    tcpClient-&gt;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-&gt;bytesAvailable() &gt; 0  )
    {
             
            qint32 packetNum  = 0;
            qint32  data = 0;
            qint32 packetSize = 0;

            in &gt;&gt;packetNum &gt;&gt; data &gt;&gt; packetSize;

            qDebug() &lt;&lt; "Packet Num : " &lt;&lt;packetNum;
            qDebug() &lt;&lt; "Data : " &lt;&lt;data;
            qDebug() &lt;&lt; "Packet Size : " &lt;&lt;packetSize;

            qDebug() &lt;&lt; "\n------------------------------------------------------------\n";
        
    }
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/93405/qtcpsocket-receives-same-packets-2-times-in-client-side</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 05:43:58 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/93405.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 06 Aug 2018 11:05:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTcpSocket receives same packets 2 times in Client Side on Mon, 06 Aug 2018 11:48:51 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/dheerendra">@<bdi>dheerendra</bdi></a> Thank you for your answer. I can not realise  this simple fault and spend too much time.<br />
Regards..</p>
]]></description><link>https://forum.qt.io/post/474222</link><guid isPermaLink="true">https://forum.qt.io/post/474222</guid><dc:creator><![CDATA[Ayse]]></dc:creator><pubDate>Mon, 06 Aug 2018 11:48:51 GMT</pubDate></item><item><title><![CDATA[Reply to QTcpSocket receives same packets 2 times in Client Side on Mon, 06 Aug 2018 11:44:08 GMT]]></title><description><![CDATA[<p dir="auto">You are writing data twice. See the following. Look at your debug statement.</p>
<p dir="auto">tcpServerConnection-&gt;write(block);<br />
qDebug() &lt;&lt; "Counter : " &lt;&lt; counter &lt;&lt; " -&gt; " &lt;&lt; tcpServerConnection-&gt;write(block);</p>
]]></description><link>https://forum.qt.io/post/474220</link><guid isPermaLink="true">https://forum.qt.io/post/474220</guid><dc:creator><![CDATA[dheerendra]]></dc:creator><pubDate>Mon, 06 Aug 2018 11:44:08 GMT</pubDate></item></channel></rss>