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

    @

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "mainwindow.h"
    4. #include <QTimer>
    5. #include <QTime>
    6. #include <QObject>
    7. #include <QFile>
    8. #include <QPalette>
    9. #include <QString>
    10. #include <QTextEdit>
    11. #include <QColor>
    12. #include <QMessageBox>
    13. #include <QGraphicsItem>
    14. #include <QtNetwork>
    15. #include "stdio.h"
    16. #include "stdlib.h"
    17. #include<memory.h>
    18. #include <QTextStream>
    19. #include <QUdpSocket>
    20. MainWindow::MainWindow(QWidget *parent) :
    21. QMainWindow(parent),
      
    22. ui(new Ui::MainWindow)
      
    23. {
    24. ui->setupUi(this); 
      
    25. udpSocketRxCDP = new QUdpSocket(this);
      
    26. udpSocketRxCDP->bind(45454);
      
    27. udpSocketRxCDP1 = new QUdpSocket(this);
      
    28. udpSocketRxCDP1->bind(45455);
      
    29. QObject::connect(udpSocketRxCDP, SIGNAL(readyRead()), this, SLOT(receiveData()));
      
    30. QObject::connect(udpSocketRxCDP1, SIGNAL(readyRead()), this, SLOT(receiveData()));
    31. ui->qLed->setColor(Qt::red);
      
    32. // QTimer *timer = new QTimer(this);
    33. // connect(timer, SIGNAL(timeout()), this, SLOT(receiveData()));
    34. // timer->start(1000);
    35. }
    36. void MainWindow::receiveData()
    37. {.
    38. bool hasWritenSomething = false;
    39. if(udpSocketRxCDP->hasPendingDatagrams())
    40. {      
      
    41.    int theSize = udpSocketRxCDP->pendingDatagramSize();
      
    42.     QByteArray datagram;
      
    43.     datagram.resize(theSize);
      
    44.     int dgsize = datagram.size();
      
    45.     udpSocketRxCDP->readDatagram(datagram.data(), dgsize);
      
    46.     QString stdg((const QByteArray&) datagram);
      
    47.    if( stdg.trimmed().length() > 0 )
      
    48.     {
      
    49.         ui->textEdit->setText(stdg);
      
    50.         hasWritenSomething = true;
      
    51.     }
      
    52. }
      
    53. if(udpSocketRxCDP1->hasPendingDatagrams())83. 
      
    54. {
      
    55.     int theSize = udpSocketRxCDP1->pendingDatagramSize();
      
    56.       QByteArray datagram;
      
    57.       datagram.resize(theSize);
      
    58.       int dgsize = datagram.size();
      
    59.       udpSocketRxCDP1->readDatagram(datagram.data(), dgsize);
      
    60.       QString stdg((const QByteArray&) datagram);
      
    61.     if( !hasWritenSomething)
      
    62.       {
      
    63.           QString stdg((const QByteArray&) datagram);
      
    64.           ui->textEdit->setText(stdg);
      
    65.       }
      
    66. }
      
    67. }
    68. MainWindow::~MainWindow()
    69. {
    70. delete ui;
      
    71. }
    72. void MainWindow::changeEvent(QEvent *e)
    73. {
    74. QMainWindow::changeEvent(e);
      
    75. switch (e->type()) {
      
    76. case QEvent::LanguageChange:
      
    77.     ui->retranslateUi(this);
      
    78.     break;
      
    79. default:
      
    80.     break;
      
    81. }
      
    82. }
    83. @@@


  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.