{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private:
Ui::Dialog *ui;
public slots:
void onReadyRead(void);
void onButtonClicked(void);
};
#define USE_QIODEVICE // remove this define to use QUDPSocket methods
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->pushBtn,SIGNAL(clicked()), this, SLOT(onButtonClicked()));
qDebug() << qVersion();
connect(sock, &QUdpSocket::readyRead, this, &Dialog::onReadyRead);
#if !defined(USE_QIODEVICE)
sock->bind();
qDebug() << sock->localPort();
#endif
}
void Dialog::onButtonClicked(void)
{
{
QList<QNetworkAddressEntry> AEList = iface.addressEntries();
{
{
#if defined(USE_QIODEVICE)
sock->bind(54366); // I have to call bind() for every interface because disconnectFromHost() unbind
sock->connectToHost(AE.broadcast(), 44444); // I have to call connectToHost to make the subsequent call to write() works
qDebug() << sock->localPort();
sock->write("hallo");
sock->disconnectFromHost(); // I have to call disconnectFromHost() otherwise the subsequent call to connectToHost() will fail
#else
dg = "hallo";
sock->writeDatagram(dg,AE.broadcast(), 44444);
#endif
qDebug() << "send to" << AE.broadcast();
}
}
}
}
void Dialog::onReadyRead(void)
{
#if defined(USE_QIODEVICE)
qDebug() << "received";
ba = sock->readAll();
#else
char data[64];
quint16 port;
while(sock->hasPendingDatagrams())
{
sock->readDatagram(data, sizeof(data), &host, &port);
qDebug() << "received";
}
#endif
// data processing...
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
QUdpSocket *sock;
public slots:
void onReadyRead(void);
void onButtonClicked(void);
};
#define USE_QIODEVICE // remove this define to use QUDPSocket methods
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->pushBtn,SIGNAL(clicked()), this, SLOT(onButtonClicked()));
qDebug() << qVersion();
sock = new QUdpSocket(this);
connect(sock, &QUdpSocket::readyRead, this, &Dialog::onReadyRead);
#if !defined(USE_QIODEVICE)
sock->bind();
qDebug() << sock->localPort();
#endif
}
void Dialog::onButtonClicked(void)
{
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface iface, list)
{
QList<QNetworkAddressEntry> AEList = iface.addressEntries();
foreach(QNetworkAddressEntry AE, AEList)
{
if(AE.broadcast().protocol() == QAbstractSocket::IPv4Protocol)
{
#if defined(USE_QIODEVICE)
sock->bind(54366); // I have to call bind() for every interface because disconnectFromHost() unbind
sock->connectToHost(AE.broadcast(), 44444); // I have to call connectToHost to make the subsequent call to write() works
qDebug() << sock->localPort();
sock->write("hallo");
sock->disconnectFromHost(); // I have to call disconnectFromHost() otherwise the subsequent call to connectToHost() will fail
#else
QByteArray dg;
dg = "hallo";
sock->writeDatagram(dg,AE.broadcast(), 44444);
#endif
qDebug() << "send to" << AE.broadcast();
}
}
}
}
void Dialog::onReadyRead(void)
{
#if defined(USE_QIODEVICE)
QByteArray ba;
qDebug() << "received";
ba = sock->readAll();
#else
char data[64];
QHostAddress host;
quint16 port;
while(sock->hasPendingDatagrams())
{
sock->readDatagram(data, sizeof(data), &host, &port);
qDebug() << "received";
}
#endif
// data processing...
}
To copy to clipboard, switch view to plain text mode
Bookmarks