Answer the question
In order to leave comments, you need to log in
Can't receive datagrams on QUdpSocket?
I'm sending a datagram via UdpSocket to my IP on the internet, but I can't catch it. Just started learning networking in Qt. Please tell me what is wrong or where to dig to solve the problem. Everything works with QHostAddress::LocalHost.
Transmitter
head1.h :
#include <QUdpSocket>
#include <QObject>
#include <QVBoxLayout>
#include <QByteArray>
#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QHostAddress>
class UDPcls : public QObject
{
Q_OBJECT
public:
QPushButton *btn_transmit = new QPushButton("Отправить");
QLineEdit *line_Edit = new QLineEdit();
QVBoxLayout *lay = new QVBoxLayout();
QWidget *window = new QWidget();
QUdpSocket *socket = new QUdpSocket();
void realiz()
{
lay->addWidget(line_Edit);
lay->addWidget(btn_transmit);
window->setLayout(lay);
window->setWindowTitle("Reciever");
window->show();
socket->bind(QHostAddress("94.25.229.127"), 0);
connect(btn_transmit, SIGNAL(clicked(bool)), this, SLOT(btn_transmit_clicked()));
}
public slots:
void btn_transmit_clicked()
{
QByteArray data;
data.append(line_Edit->text());
socket->writeDatagram(data, QHostAddress("94.25.229.127"), 0);
line_Edit->setText("Датаграмма отправлена");
}
};
#include "head.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UDPcls UDPobj;
UDPobj.realiz();
return a.exec();
}
#include <QUdpSocket>
#include <QObject>
#include <QByteArray>
#include <QWidget>
#include <QLabel>
class UDPcls : public QObject
{
Q_OBJECT
public:
QLabel *lbl = new QLabel("Ожидание датграммы...");
QUdpSocket *socket = new QUdpSocket();
void realiz()
{
lbl->show();
socket->bind(QHostAddress("94.25.229.127"), 0);
connect(socket, SIGNAL(readyRead()), this, SLOT(reciev()));
}
public slots:
void reciev()
{
QByteArray data;
data.resize(socket->pendingDatagramSize());
socket->readDatagram(data.data(), data.size());
lbl->setText(data);
}
};
#include "head.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UDPcls UDPobj;
UDPobj.realiz();
return a.exec();
}
Answer the question
In order to leave comments, you need to log in
I watch you from SPb.
First, check the port forwarding on the remote machine. Forwarding is done in the router. Secondly, if memory serves me, Yota gives a gray IP, not a white one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question