📄 7.13 实例:收发广播报文.txt
字号:
#ifndef RECEIVER_H
#define RECEIVER_H
#include <QDialog>
class QLabel;
class QPushButton;
class QUdpSocket;
class Receiver : public QDialog
{
Q_OBJECT
public:
Receiver(QWidget *parent = 0);
private slots:
void processPendingDatagrams();
private:
QLabel *statusLabel;
QPushButton *quitButton;
QUdpSocket *udpSocket;
};
#endif
#include <QtGui>
#include <QtNetwork>
#include "receiver.h"
Receiver::Receiver(QWidget *parent)
: QDialog(parent)
{
statusLabel = new QLabel(tr("Listening for broadcasted messages"));
quitButton = new QPushButton(tr("&Quit"));
udpSocket = new QUdpSocket(this);
udpSocket->bind(45454);
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("Broadcast Receiver"));
}
void Receiver::processPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
statusLabel->setText(tr("Received datagram: \"%1\"")
.arg(datagram.data()));
}
}
#include <QApplication>
#include "receiver.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Receiver receiver;
receiver.show();
return receiver.exec();
}
#ifndef SENDER_H
#define SENDER_H
#include <QDialog>
class QDialogButtonBox;
class QLabel;
class QPushButton;
class QTimer;
class QUdpSocket;
class Sender : public QDialog
{
Q_OBJECT
public:
Sender(QWidget *parent = 0);
private slots:
void startBroadcasting();
void broadcastDatagram();
private:
QLabel *statusLabel;
QPushButton *startButton;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QUdpSocket *udpSocket;
QTimer *timer;
int messageNo;
};
#endif
#include <QtGui>
#include <QtNetwork>
#include "sender.h"
Sender::Sender(QWidget *parent)
: QDialog(parent)
{
statusLabel = new QLabel(tr("Ready to broadcast datagrams on port 45454"));
startButton = new QPushButton(tr("&Start"));
quitButton = new QPushButton(tr("&Quit"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
timer = new QTimer(this);
udpSocket = new QUdpSocket(this);
messageNo = 1;
connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Broadcast Sender"));
}
void Sender::startBroadcasting()
{
startButton->setEnabled(false);
timer->start(1000);
}
void Sender::broadcastDatagram()
{
statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo));
QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo);
udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, 45454);
++messageNo;
}
#include <QApplication>
#include "sender.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Sender sender;
sender.show();
return sender.exec();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -