UDP Read/Write fail



  • 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"?


  • Moderators

    @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.



  • #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?





  • @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?



  • 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


  • Moderators

    @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.


Log in to reply
 

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