UDP 用户数据报协议,是一种轻量级、无连接,不可靠,数据报的传输层协议。
一般用在短消息、广播消息等中,本文中是用在广播定时发送消息中。
注意还是要在.pro中加入 QT += network
广播定时发送给用户数据:
1、UDP的服务器端
先创建一个QUdpSocket对象,再创建一个定时器QTimer每秒发送一次数据,
最后利用QUdpSocket的writeDatagram()函数来设置地址和绑定端口来发送数据。
2、UDP的客服端
创建一个绑定好端口的QUdpSocket对象,此时最重要的一句话
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived())); //其中dataReceived是自定义的函数,接受数据函数
当QUdpSocket对象每次要读入一个新数据的时候,就发送信号readyRead();
再来就是编写我们自定义的接受数据函数了,利用QUdpSocket的readDatagram()函数即可获取数据。
截图:
服务端:
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include
class QLabel;
class QLineEdit;
class QUdpSocket;
class UdpServer: public QDialog
{
Q_OBJECT
private:
int port;
bool isStarted;
QUdpSocket *udpSocket;
QTimer *timer;
QLabel *timerLabel;
QLineEdit *contEdit;
QPushButton *startButton;
public:
UdpServer(QWidget *parent = 0);
private slots:
void slotStartButton();
void timeoutDo();
};
#endif // UDPSERVER_H
#include "udpserver.h"
#include
#include
UdpServer::UdpServer(QWidget *parent)
: QDialog(parent)
{
port = 5555;
isStarted = false;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this);
//设置定时器,当时间到的时候就调用timeoutDo()槽
connect(timer, SIGNAL(timeout()), this, SLOT(timeoutDo()));
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
timerLabel = new QLabel(tr("定时发送"));
contEdit = new QLineEdit;
startButton = new QPushButton(tr("开始"));
connect(startButton, SIGNAL(clicked()),
this, SLOT(slotStartButton()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(timerLabel);
mainLayout->addWidget(contEdit);
mainLayout->addWidget(startButton);
setLayout(mainLayout);
setWindowTitle(tr("UDP Server"));
}
void UdpServer::slotStartButton()
{
if(false == isStarted) {
startButton->setText(tr("停止"));
isStarted = true;
timer->start(1000);
}
else {
startButton->setText(tr("开始"));
isStarted = false;
timer->stop();
}
}
void UdpServer::timeoutDo()
{
QString msg = contEdit->text();
int length = 0;
if("" == msg)
return ;
else if((length = udpSocket->writeDatagram(
msg.toLatin1(), msg.length(),
QHostAddress::Broadcast, port)) != msg.length()) {
return ;
}
}
#include "udpserver.h"
#include
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
UdpServer *server = new UdpServer;
server->show();
return app.exec();
}
//客服端:
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include
class QTextEdit;
class QUdpSocket;
class UdpClient: public QDialog
{
Q_OBJECT
private:
int port;
QUdpSocket *udpSocket;
QTextEdit *showText;
QPushButton *closeButton;
public:
UdpClient(QWidget *parent = 0);
private slots:
void slotClose();
void dataReceived();
};
#endif // UDPCLIENT_H
#include "udpclient.h"
#include
#include
UdpClient::UdpClient(QWidget *parent)
: QDialog(parent)
{
port = 5555;
udpSocket = new QUdpSocket(this);
bool result = udpSocket->bind(port);
if(!result) {
QMessageBox::information(this, tr("error"),
tr("udp socket create error!"));
return ;
}
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(dataReceived()));
showText = new QTextEdit;
showText->setReadOnly(true);
closeButton = new QPushButton(tr("Close"));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(slotClose()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(showText);
mainLayout->addWidget(closeButton);
setLayout(mainLayout);
setWindowTitle(tr("UDP Client"));
}
//private slots
void UdpClient::slotClose()
{
udpSocket->close();
}
void UdpClient::dataReceived()
{
while(udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
showText->insertPlainText(datagram.data());
}
}
#include "udpclient.h"
#include
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
UdpClient *client = new UdpClient;
client->show();
return app.exec();
}