Udp receiver
-
Hi frnds i am trying to develop a redundant udp receiver which should receive the data from two senders. There are two senders which are sending same data at every one second. The client has to receive the data from only one source at a time and display in Widgets.
My problem is that when i receive data at every 1 sec interval then i miss some data and the receive delayed data. Thts why i have to query at every 200ms to get correct data. BUT when i use 200ms timer then i see that receiver is not receiving data from only one source but it receives from second also which should not happen.
@#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QtGui>
#include <QtNetwork>
#include<QTimer>static int leddisplay_status =0 ,change_flag1=60,leddisplay_status1=0;
char databuf[1024];
char databuf1[1024];
int bytesRead,bytesRead1;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); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(receiveData())); timer->start(1000);
}
void MainWindow::receiveData()
{
bool firstwritten=false;
if(udpSocketRxCDP->hasPendingDatagrams())
{
bytesRead=-1;
bytesRead= udpSocketRxCDP->readDatagram(databuf,sizeof(databuf));
ui->pushButton->setText(databuf);
firstwritten=true;
}if(udpSocketRxCDP1->hasPendingDatagrams())
{
bytesRead1=-1;
bytesRead1= udpSocketRxCDP1->readDatagram(databuf1,sizeof(databuf1));if(firstwritten==false)
ui->pushButton->setText(databuf1);}
}
@This happens because the sender sends data at 1000ms interval but the receiver queries for data at every
200ms thts why in between the interval the conditions given in code fails and receiver receives from the second source also.Can any body please tell me how to solve this problem. Thank u all in advance
-
Hi,
IMHO your design is very "error prone"; your problem isn't a receive problem but a refresh problem.
I suggest to:
- receive data once it is avalaible on Socket (connect readyRead() signal) and store received data
- every seconds choose which data show in your GUI and clear stored values
In this way you're sure to not lost data and to show the last arrived values.