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 <QtNetwork>

    static int leddisplay_status =0 ,change_flag1=60,leddisplay_status1=0;
    char databuf[1024];
    char databuf1[1024];
    int bytesRead,bytesRead1;

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    udpSocketRxCDP = new QUdpSocket(this);
    udpSocketRxCDP1 = new QUdpSocket(this);
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(receiveData()));


    void MainWindow::receiveData()
    bool firstwritten=false;
    bytesRead= udpSocketRxCDP->readDatagram(databuf,sizeof(databuf));

    bytesRead1= udpSocketRxCDP1->readDatagram(databuf1,sizeof(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.

Log in to reply