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""
Forum Updated to NodeBB v4.3 + New Features

READ DATA FROM SPECIFIC UDP ""PORT""

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 423 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 17 Mar 2020, 14:14 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 17 Mar 2020, 15:27 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
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 18 Mar 2020, 01:13 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.

        1 Reply Last reply
        0

        1/3

        17 Mar 2020, 14:14

        • Login

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