Network data Receive Delay in buffer
-
Hi frnds i am new to Qt . I am working on receiving data in Desktop application and displaying them in readtime on EditTxt.
I have to sources of data comming on two different ports. Both are having same data. Beloo is the code for receiver.Which should receive data redundantly. ie if first source fails then it reads from another and displays the.
Problem is that when the first source is closed the it starts receiving from another BUT not the current data but some older data from second source. I have tried using clearing biffer on every timer tick but even then it is not displaying the current data.Plz suggest any solution
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QTimer>
#include <QTime>
#include <QObject>
#include <QFile>
#include <QPalette>
#include <QString>
#include <QTextEdit>
#include <QColor>
#include <QMessageBox>
#include <QGraphicsItem>
#include <QtNetwork>
#include "stdio.h"
#include "stdlib.h"
#include<memory.h>
#include <QTextStream>
#include <QUdpSocket>int bytesRead;
char databuf[30];
char databuf1[30];MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow){
ui->setupUi(this);udpSocketRxCDP = new QUdpSocket(this); udpSocketRxCDP->bind(45454); udpSocketRxCDP1 = new QUdpSocket(this); udpSocketRxCDP1->bind(45455); ui->qLed->setColor(Qt::red); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(receiveData())); timer->start(1000);
}
void MainWindow::receiveData()
{
for(int j=0;j<30;j++)
databuf1[j]=0;for(int i=0;i<30;i++) databuf[i]=0; //if(udpSocketRxCDP->hasPendingDatagrams()&& !(udpSocketRxCDP1->hasPendingDatagrams())) if(udpSocketRxCDP->hasPendingDatagrams()) { bytesRead=-1; ui->textEdit->setText(""); bytesRead = udpSocketRxCDP->readDatagram(databuf, sizeof(databuf)); ui->textEdit->setText(databuf); ui->qLed->setColor(Qt::green); } else if(udpSocketRxCDP1->hasPendingDatagrams()) { bytesRead=-1; ui->textEdit->setText(""); bytesRead = udpSocketRxCDP1->readDatagram(databuf1, sizeof(databuf1)); ui->textEdit->setText(databuf1); ui->qLed->setColor(Qt::green); }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}@@@
-
hello,
i see you do a timed read of udpSocketRxCDP and if you can't find anything look in udpSocketRxCDP1
if both sockets receive data i think you should handle both ? otherwise the buffer of udpSocketRxCDP1 will never get cleared.
my suggestion is to not use a timer but an event driven handler like:
@connect(&udpSocketRxCDP, SIGNAL(readyRead()),this,SLOT(receiveData()));
connect(&udpSocketRxCDP1, SIGNAL(readyRead()),this,SLOT(receiveData()));
@
and loose the "else" bit in-else- if(udpSocketRxCDP1->hasPendingDatagrams())
-
note: you can actually request the size of the pending data like:
@ int theSize = udpSocketRxCDP.pendingDatagramSize();
QByteArray datagram; datagram.resize(theSize); int dgsize = datagram.size(); udpSocketRxCDP.readDatagram(datagram.data(), dgsize); QString stdg((const QByteArray&) datagram); ui->textEdit->setText(stdg);@
-
[quote author="clogwog" date="1375188412"]note: you can actually request the size of the pending data like:
@ int theSize = udpSocketRxCDP.pendingDatagramSize();
QByteArray datagram; datagram.resize(theSize); int dgsize = datagram.size(); udpSocketRxCDP.readDatagram(datagram.data(), dgsize); QString stdg((const QByteArray&) datagram); ui->textEdit->setText(stdg);@
[/quote]
Hi clogwog Thank u very much for giving suggestion. As u Told i have implemented the code like belo. I found that now there is no delay problem BUT both sockets are writing the data to editText ie the data from
both sources are overlapping but i want to display the data from only one at a time in EditText. Kindly suggest what should i do so that data from only one source be displayed in EditText at a Time.
Thanking u in advance.@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include <QTimer>
#include <QTime>
#include <QObject>
#include <QFile>
#include <QPalette>
#include <QString>
#include <QTextEdit>
#include <QColor>
#include <QMessageBox>
#include <QGraphicsItem>
#include <QtNetwork>
#include "stdio.h"
#include "stdlib.h"
#include<memory.h>
#include <QTextStream>
#include <QUdpSocket>int bytesRead;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);udpSocketRxCDP = new QUdpSocket(this); udpSocketRxCDP->bind(45454); udpSocketRxCDP1 = new QUdpSocket(this); udpSocketRxCDP1->bind(45455); QObject::connect(udpSocketRxCDP, SIGNAL(readyRead()), this, SLOT(receiveData())); QObject::connect(udpSocketRxCDP1, SIGNAL(readyRead()), this, SLOT(receiveData()));
}
void MainWindow::receiveData()
{
if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);
QString stdg((const QByteArray&) datagram);
ui->textEdit->setText(stdg);
}if(udpSocketRxCDP1->hasPendingDatagrams()) { int theSize = udpSocketRxCDP1->pendingDatagramSize(); QByteArray datagram; datagram.resize(theSize); int dgsize = datagram.size(); udpSocketRxCDP1->readDatagram(datagram.data(), dgsize); QString stdg((const QByteArray&) datagram); ui->textEdit->setText(stdg); }
}
MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}@
-
how about ?
@void MainWindow::receiveData()
{
bool hasWritenSomething = false;
if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);
QString stdg((const QByteArray&) datagram);
if( stdg.trim().length() > 0 )
{
ui->textEdit->setText(stdg);
hasWritenSomething = true;
}
}if(udpSocketRxCDP1->hasPendingDatagrams()) { int theSize = udpSocketRxCDP1->pendingDatagramSize(); QByteArray datagram; datagram.resize(theSize); int dgsize = datagram.size(); udpSocketRxCDP1->readDatagram(datagram.data(), dgsize); if( !hasWritenSomething) { QString stdg((const QByteArray&) datagram); ui->textEdit->setText(stdg); } }@
-
[quote author="clogwog" date="1375229735"]how about ?
@void MainWindow::receiveData()
{
bool hasWritenSomething = false;
if(udpSocketRxCDP->hasPendingDatagrams())
{
int theSize = udpSocketRxCDP->pendingDatagramSize();
QByteArray datagram;
datagram.resize(theSize);
int dgsize = datagram.size();
udpSocketRxCDP->readDatagram(datagram.data(), dgsize);
QString stdg((const QByteArray&) datagram);
if( stdg.trim().length() > 0 )
{
ui->textEdit->setText(stdg);
hasWritenSomething = true;
}
}if(udpSocketRxCDP1->hasPendingDatagrams()) { int theSize = udpSocketRxCDP1->pendingDatagramSize(); QByteArray datagram; datagram.resize(theSize); int dgsize = datagram.size(); udpSocketRxCDP1->readDatagram(datagram.data(), dgsize); if( !hasWritenSomething) { QString stdg((const QByteArray&) datagram); ui->textEdit->setText(stdg); } }@[/quote]
@
Hi clogwog thank u very much for your reply.I have implemented the code as directed by you BUT found that again Both sources are writting
the data to Edittext Simultaneously.The two sources are sending the data at every one second interval. I doubt that in between the intervals our condition of flag is not working.
When i fired the receiveData method using the 1000ms timer then i found that it was working fine because it cheks at every 1000ms and at that particular interval only one condition becomes true.It was my observation But please give ur suggestion for better solution.
Thanking u in advance@
3. #include "mainwindow.h"
4. #include "ui_mainwindow.h"
5. #include "mainwindow.h"
6. #include <QTimer>
7. #include <QTime>
8. #include <QObject>
9. #include <QFile>
10. #include <QPalette>
11. #include <QString>
12. #include <QTextEdit>
13. #include <QColor>
14. #include <QMessageBox>
15. #include <QGraphicsItem>
16. #include <QtNetwork>
17. #include "stdio.h"
18. #include "stdlib.h"
19. #include<memory.h>
20. #include <QTextStream>
21. #include <QUdpSocket>
30. MainWindow::MainWindow(QWidget *parent) :
31. QMainWindow(parent),
32. ui(new Ui::MainWindow)
33.
34.
35. {
36. ui->setupUi(this);
38.
39. udpSocketRxCDP = new QUdpSocket(this);
40. udpSocketRxCDP->bind(45454);
41.
42. udpSocketRxCDP1 = new QUdpSocket(this);
43. udpSocketRxCDP1->bind(45455);
44.
45. QObject::connect(udpSocketRxCDP, SIGNAL(readyRead()), this, SLOT(receiveData()));
46.
47. QObject::connect(udpSocketRxCDP1, SIGNAL(readyRead()), this, SLOT(receiveData()));
48. ui->qLed->setColor(Qt::red);
49.
50. // QTimer *timer = new QTimer(this);
51. // connect(timer, SIGNAL(timeout()), this, SLOT(receiveData()));
52. // timer->start(1000);
53.
54.
55. }
56.
57.
58.
59.
60. void MainWindow::receiveData()
61. {.
63. bool hasWritenSomething = false;
64. if(udpSocketRxCDP->hasPendingDatagrams())
65.
66. {
67. int theSize = udpSocketRxCDP->pendingDatagramSize();
68. QByteArray datagram;
69. datagram.resize(theSize);
70. int dgsize = datagram.size();
71. udpSocketRxCDP->readDatagram(datagram.data(), dgsize);
72. QString stdg((const QByteArray&) datagram);
73.
74. if( stdg.trimmed().length() > 0 )
75. {
76. ui->textEdit->setText(stdg);
77. hasWritenSomething = true;
78. }
79.
80. }
81.
82. if(udpSocketRxCDP1->hasPendingDatagrams())83.
84. {
85. int theSize = udpSocketRxCDP1->pendingDatagramSize();
86. QByteArray datagram;
87. datagram.resize(theSize);
88. int dgsize = datagram.size();
89. udpSocketRxCDP1->readDatagram(datagram.data(), dgsize);
90. QString stdg((const QByteArray&) datagram);
91.
92.
93. if( !hasWritenSomething)
94. {
95. QString stdg((const QByteArray&) datagram);
96. ui->textEdit->setText(stdg);
97. }
98.
99. }
100.
101. }
102.
103.
104.
105.
106.
107.
108. MainWindow::~MainWindow()
109. {
110. delete ui;
111. }
112.
113. void MainWindow::changeEvent(QEvent *e)
114. {
115. QMainWindow::changeEvent(e);
116. switch (e->type()) {
117. case QEvent::LanguageChange:
118. ui->retranslateUi(this);
119. break;
120. default:
121. break;
122. }
123. }
124. @@@ -
mmm so you receive data on both sockets all the time.
but you would only like to show the data from the second socket once you haven't received data from the first socket for a particular time ?how about a timer that gets reset every time you receive data from the first socket ? and only when the timer expires you start looking at data from the second socket ?
-
[quote author="clogwog" date="1375254942"]mmm so you receive data on both sockets all the time.
but you would only like to show the data from the second socket once you haven't received data from the first socket for a particular time ?how about a timer that gets reset every time you receive data from the first socket ? and only when the timer expires you start looking at data from the second socket ?
[/quote]
Ok Sir Thank u very much for the precious guidance that u have given to me. I learned a lot of new things from your post. Thank u very much .have a nice day