Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. UDP Read/Write fail
Forum Updated to NodeBB v4.3 + New Features

UDP Read/Write fail

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Manaan
    wrote on last edited by Manaan
    #1

    Okay, I think I need some clarification on how UDP works, 'cause I couldn't find good answers myself.

    What I have is an UDP text editor, where 2 people can edit text simultaniously.
    What I do for each window is create a socket, bind it and connect it to my ready_Read function, where readDatagram() is used.
    Then, on_plainTextEdit_textChanged I run my ready_Write function.

    Everything works fine in one window - UDP sends data, then it recieves it(hears what it said itself?), debug log says what has been recieved, and from what port. But everything breaks if I compile .exe and run 2 of them. The first one always supports both read and write - if you write anything in the second window it will be shown in the first. But the second window is "deaf" - you can write in it, and the text will be sent, but writing in the first window will not trigger any changes to the second one.

    Any ideas on why does it behave like that? Maybe I have to change something in my QUDPSocket, or just try and run the app on 2 different machines linked via LAN?
    What's causing second window's "deafness"?

    K 1 Reply Last reply
    0
    • M Manaan

      Okay, I think I need some clarification on how UDP works, 'cause I couldn't find good answers myself.

      What I have is an UDP text editor, where 2 people can edit text simultaniously.
      What I do for each window is create a socket, bind it and connect it to my ready_Read function, where readDatagram() is used.
      Then, on_plainTextEdit_textChanged I run my ready_Write function.

      Everything works fine in one window - UDP sends data, then it recieves it(hears what it said itself?), debug log says what has been recieved, and from what port. But everything breaks if I compile .exe and run 2 of them. The first one always supports both read and write - if you write anything in the second window it will be shown in the first. But the second window is "deaf" - you can write in it, and the text will be sent, but writing in the first window will not trigger any changes to the second one.

      Any ideas on why does it behave like that? Maybe I have to change something in my QUDPSocket, or just try and run the app on 2 different machines linked via LAN?
      What's causing second window's "deafness"?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @Manaan

      You have to post some source that people can try to understand how youz are doing what have already described.

      It might be even the case that you are not sending to the outside of application at all.

      Did you checkout the MulticastSender and MulticastReceiver examples?
      They should do almost what you try and be therefore a good starting point.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      3
      • M Offline
        M Offline
        Manaan
        wrote on last edited by
        #3
        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QtNetwork>
        #include <myudp.h>
        
        
        
        
        MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            
            socket = new QUdpSocket(this);
            socket->bind(QHostAddress::LocalHost, 4155);
        
            connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
            prevText = ui->plainTextEdit->toPlainText();
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::HelloUDP()
        {
            QByteArray Data;
            Data.append("Hello from UDP");
        
            // Sends the datagram datagram
            // to the host address and at port.
            // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
            //                      const QHostAddress & host, quint16 port)
            socket->writeDatagram(Data, QHostAddress::LocalHost, 4155);
        
        }
        
        
        
        void MainWindow::UDPSend(QString dataToSend, qint64 port)
        {
            QByteArray Data;
            Data.append(dataToSend);
        
            // Sends the datagram datagram
            // to the host address and at port.
            // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
            //                      const QHostAddress & host, quint16 port)
            ui->label->setText(ui-> label->text() + " Sending Data. Yay:3");
            socket->writeDatagram(Data, QHostAddress::LocalHost, port);
        }
        void MainWindow::readyRead()
        {
            // when data comes in
            ui->label->setText(ui-> label->text() + " Got some data. Yay:3");
            QByteArray buffer;
            buffer.resize(socket->pendingDatagramSize());
        
            QHostAddress sender;
            quint16 senderPort;
        
            socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
        
            qDebug() << "Message from: " << sender.toString();
            qDebug() << "Message port: " << senderPort;
            qDebug() << "Message: " << buffer;
        
        
            ChangeText(buffer);
        
        }
        
        
        
        void MainWindow::on_plainTextEdit_textChanged()
        {
            if(ui->plainTextEdit->toPlainText() != prevText){
        
             MainWindow::UDPSend(ui->plainTextEdit->toPlainText(), 4155);
             prevText = ui->plainTextEdit->toPlainText();
        
        
        }
        }
        
        void MainWindow::ChangeText(QString text){
        
            int pt = ui->plainTextEdit->textCursor().position();
            int offset = ui->plainTextEdit->toPlainText().length() - pt;
            QTextCursor tmpCursor = ui->plainTextEdit->textCursor();
        
            ui->plainTextEdit->setPlainText(text);
        
        
            ui->plainTextEdit_2->setPlainText(text);
        
            qDebug() << pt;
        
            ui->plainTextEdit->setTextCursor(tmpCursor);
            tmpCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, offset);
            ui->plainTextEdit->setTextCursor(tmpCursor);
        }
        
        

        that's the code. I wasn't checking Multicast examples, and I'm pretty sure that the app actually SENDS the data outside - writing in the "deaf" window sends the data to the "normal" window, but not vise versa. I think it may be bind() or something else preventing the second window from listening. A port is already taken(by the first window) or something?

        JonBJ 1 Reply Last reply
        0
        • M Manaan
          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QtNetwork>
          #include <myudp.h>
          
          
          
          
          MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent), ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              
              socket = new QUdpSocket(this);
              socket->bind(QHostAddress::LocalHost, 4155);
          
              connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
              prevText = ui->plainTextEdit->toPlainText();
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::HelloUDP()
          {
              QByteArray Data;
              Data.append("Hello from UDP");
          
              // Sends the datagram datagram
              // to the host address and at port.
              // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
              //                      const QHostAddress & host, quint16 port)
              socket->writeDatagram(Data, QHostAddress::LocalHost, 4155);
          
          }
          
          
          
          void MainWindow::UDPSend(QString dataToSend, qint64 port)
          {
              QByteArray Data;
              Data.append(dataToSend);
          
              // Sends the datagram datagram
              // to the host address and at port.
              // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
              //                      const QHostAddress & host, quint16 port)
              ui->label->setText(ui-> label->text() + " Sending Data. Yay:3");
              socket->writeDatagram(Data, QHostAddress::LocalHost, port);
          }
          void MainWindow::readyRead()
          {
              // when data comes in
              ui->label->setText(ui-> label->text() + " Got some data. Yay:3");
              QByteArray buffer;
              buffer.resize(socket->pendingDatagramSize());
          
              QHostAddress sender;
              quint16 senderPort;
          
              socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
          
              qDebug() << "Message from: " << sender.toString();
              qDebug() << "Message port: " << senderPort;
              qDebug() << "Message: " << buffer;
          
          
              ChangeText(buffer);
          
          }
          
          
          
          void MainWindow::on_plainTextEdit_textChanged()
          {
              if(ui->plainTextEdit->toPlainText() != prevText){
          
               MainWindow::UDPSend(ui->plainTextEdit->toPlainText(), 4155);
               prevText = ui->plainTextEdit->toPlainText();
          
          
          }
          }
          
          void MainWindow::ChangeText(QString text){
          
              int pt = ui->plainTextEdit->textCursor().position();
              int offset = ui->plainTextEdit->toPlainText().length() - pt;
              QTextCursor tmpCursor = ui->plainTextEdit->textCursor();
          
              ui->plainTextEdit->setPlainText(text);
          
          
              ui->plainTextEdit_2->setPlainText(text);
          
              qDebug() << pt;
          
              ui->plainTextEdit->setTextCursor(tmpCursor);
              tmpCursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, offset);
              ui->plainTextEdit->setTextCursor(tmpCursor);
          }
          
          

          that's the code. I wasn't checking Multicast examples, and I'm pretty sure that the app actually SENDS the data outside - writing in the "deaf" window sends the data to the "normal" window, but not vise versa. I think it may be bind() or something else preventing the second window from listening. A port is already taken(by the first window) or something?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Manaan
          https://stackoverflow.com/questions/17143989/how-to-have-more-processes-listen-on-the-same-port-of-the-same-machine-with-qudp ?

          M 1 Reply Last reply
          1
          • JonBJ JonB

            @Manaan
            https://stackoverflow.com/questions/17143989/how-to-have-more-processes-listen-on-the-same-port-of-the-same-machine-with-qudp ?

            M Offline
            M Offline
            Manaan
            wrote on last edited by
            #5

            @JonB Thanks for your reply. From what I read, Multicast is an option, I'll give it a try.

            But there is another question - when run, my app starts listening on 4155 port, effectively blocking any other apps from using it. But what if I run apps in 2 different machines(don't have a chance to check that myself right now)?
            I guess that it shall use 4155 on my machine, while the second will use the same unblocked 4155 on the other - this looks good, but will this work?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Manaan
              wrote on last edited by
              #6

              Now testing on two machines via LAN - both apps run and can hear themselves, but not each other. Guess I have to use address other then 127.0.0.1 or 0.0.0.0

              K 1 Reply Last reply
              0
              • M Manaan

                Now testing on two machines via LAN - both apps run and can hear themselves, but not each other. Guess I have to use address other then 127.0.0.1 or 0.0.0.0

                K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                @Manaan

                Yes, 127.0.0.1 is your local machine and you do not get anywhere. And 0.0.0.0 is not good idea either.

                Check the different IP addresses on your router.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved