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. READ DATA FROM SPECIFIC UDP ""PORT""
Qt 6.11 is out! See what's new in the release blog

READ DATA FROM SPECIFIC UDP ""PORT""

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 585 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
    Marco Flad
    wrote on last edited by aha_1980
    #1

    I have a device on the network that sends and Receive UDP packets on port = 26000 I can send to it but cant receive from the Device Because i cant specify the receiving port = 26000 on my program.
    every time i close - open the program the program sending on different port (Randomly change).
    i figured it out by use a packet sender program :

    i Send the packet From My PC and Received on "PACKET SENDER"
    From My IP And random Port
    then i send the packet back from "Packet Sender " to my IP and "This random Port and successfully received it on my program

    so there is away to Receive the data on specific port .
    CODE :

    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
         socket = new QUdpSocket(this);     
         socket->bind(QHostAddress("192.168.50.55"),26000);  
         connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    
    }
    
    Widget::~Widget()
    {
        delete ui;
        socket->close();
    }
    
    
    void Widget::readyRead()
    {
    
        // when data comes in
         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;
         qDebug() << sizeof (buffer);
    
         ui->textEdit->append(buffer.toHex());
    
    }
    

    thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Marco Flad
      wrote on last edited by
      #2

      i solved it by using QUDPSocket function derived from QAbstractSocket:

      socket->bind(26000, QUdpSocket::ShareAddress);
      

      instead of :

      socket->bind(QHostAddress("192.168.50.55"),26000);
      
      1 Reply Last reply
      1
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        Try your original code with the address of 0.0.0.0

        I believe you may need privilege to bind to a specific interface.

        The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

        1 Reply Last reply
        0

        • Login

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