×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    2

    Problem readyRead() QUdpSocket

    the execution of this program gives one result of trade in the first click and the second click on the button the message is sent but the reception does not work.
    I think that problem is in function “ processPendingDatagrams() “ or readyRead().
    helps please.

    Code:
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QRadioButton>
         
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
         
            ui->setupUi(this);
            //******** création des sockets ***********
            talkerSocket = new QUdpSocket(this);
            listenSocket = new QUdpSocket(this);
         
            if (!listenSocket->bind(QHostAddress::Any, 8888)) {
               qDebug("Impossible de créer le socket en écoute");
               exit(EXIT_FAILURE);
            }
         
            qDebug() <<"Connecting...";
         
            talkerSocket->connectToHost(QHostAddress::LocalHost,8888);
         
         
            if(!talkerSocket->waitForConnected(1000))
            {
                qDebug() <<"ERROR : "<<talkerSocket->errorString();
            }
         
            ui->statusBar->showMessage("connecté");
         
            QObject::connect(listenSocket, SIGNAL(readyRead()),
                            this, SLOT(processPendingDatagrams()));
        }
         
        MainWindow::~MainWindow()
        {
            delete ui;
        }
         
        void MainWindow::on_alerte_clicked()
        {
         
            QByteArray datagram;
         
            QString msg = "alert";
         
            datagram.append(msg);
         
            if (talkerSocket->writeDatagram(datagram, senderAddress, senderPort) == -1) {
                qDebug("Émission du message impossible");
                talkerSocket->close();
                exit(EXIT_FAILURE);
            }
         
            datagram.clear();
            qDebug()<<"envoie succee";
         
        }
         
        void MainWindow::processPendingDatagrams()
        {
            while (listenSocket->hasPendingDatagrams())  {
                qDebug()<<"il existe un datagrame";
         
                QByteArray datagram;
         
                datagram.resize(listenSocket->pendingDatagramSize());
                if (listenSocket->readDatagram(datagram.data(), datagram.size(),
                                           &senderAddress, &senderPort) == -1) {
                    listenSocket->close();
                    exit(EXIT_FAILURE);
                }
         
                QString msge = datagram.data();
                qDebug() << "Depuis : " << senderAddress.toString() << ':' << senderPort;
                qDebug() << "Message : " << msge;
         
                ui->label->setText(tr("Depuis : %1: %2")
                                         . arg(senderAddress.toString())
                                         . arg(senderPort));
         
                qDebug()<<"reception succé";
                datagram.clear();
         
            }
         
        }

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,694

    Re: Problem readyRead() QUdpSocket

    senderPort gets overwritten on reception, then the second datagram is sent to that port - but the inbound socket still listens on 8888. In fact senderAddress is overwritten as well, but that would still work internally, on a single host.

Similar Threads

  1. Question for qudpsocket class ???!
    By CoaValoVa in forum [Archived] Beginning Qt
    Replies: 2
    Last Post: 2011-03-19, 01:56
  2. QUdpSocket problems on windows?
    By candinico in forum Qt
    Replies: 2
    Last Post: 2011-01-27, 10:23
  3. Replies: 4
    Last Post: 2010-12-13, 06:18
  4. http signal:readyRead
    By chensu0526 in forum Qt
    Replies: 2
    Last Post: 2010-11-15, 01:50
  5. Replies: 1
    Last Post: 2010-08-12, 10:27

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×