Unsolved READ DATA FROM SPECIFIC UDP ""PORT""
-
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 programso 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
-
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);
-
Try your original code with the address of 0.0.0.0
I believe you may need privilege to bind to a specific interface.