服务器端
初始化中创建QUdpSocket、QTimer,当计时器超时,向客户端发送文本框中的数据。计时器每隔1s重启一次。
.pro
QT += network
udpserver.h
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UdpServer: public QDialog
{
Q_OBJECT
public:
UdpServer(QWidget *parent=0, Qt::WindowFlags f = 0);
~UdpServer();
public slots:
void StartBtnClicked();
void timeout();
private:
QLabel *TimerLabel;
QLineEdit *TextLineEdit;
QPushButton *StartBtn;
QVBoxLayout *mainLayout;
int port;
bool isStarted;
QUdpSocket *udpSocket;
QTimer *timer;
};
#endif // UDPSERVER_H
udpserver.cpp
#include "udpserver.h"
#include <QHostAddress>
UdpServer::UdpServer(QWidget *parent, Qt::WindowFlags f)
{
setWindowTitle(tr("UDP Server"));
TimerLabel = new QLabel(tr("计时器:"));
TextLineEdit = new QLineEdit(this);
StartBtn = new QPushButton(tr("开始"), this);
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(TimerLabel);
mainLayout->addWidget(TextLineEdit);
mainLayout->addWidget(StartBtn);
connect(StartBtn, SIGNAL(clicked()), this, SLOT(StartBtnClicked()));
isStarted = false;
port = 5555;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timeout()));
}
UdpServer::~UdpServer()
{
}
void UdpServer::StartBtnClicked()
{
if(!isStarted)
{
StartBtn->setText(tr("停止"));
timer->start(1000); //每隔1秒启动一次
isStarted = true;
}
else
{
StartBtn->setText(tr("开始"));
isStarted = false;
timer->stop();
}
}
//向端口发送广播信息
void UdpServer::timeout()
{
QString msg = TextLineEdit->text();
int length = 0;
if(msg == "")
return;
if((length == udpSocket->writeDatagram(msg.toLatin1(),
msg.length(), QHostAddress::Broadcast, port)) != msg.length())
return;
}
main.cpp
#include "udpserver.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpServer w;
w.show();
return a.exec();
}
客户端
初始化后中创建QUdpSocket,绑定端口,当接收到数据时,显示到文本框中
.pro
QT += network
udpclient.h
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>
class UdpClient:public QDialog
{
Q_OBJECT
public:
UdpClient(QWidget *parent=0, Qt::WindowFlags f = 0);
~UdpClient();
public slots:
void CloseBtnClicked();
void dataReceived();
private:
QTextEdit *ReceiveTextEdit;
QPushButton *CloseBtn;
QVBoxLayout *mainLayout;
int port;
QUdpSocket *udpSocket;
};
#endif // UDPCLIENT_H
udpserver.cpp
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent, Qt::WindowFlags f):QDialog(parent)
{
setWindowTitle(tr("UDP Client"));
ReceiveTextEdit = new QTextEdit(this);
CloseBtn = new QPushButton(tr("Close"), this);
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(ReceiveTextEdit);
mainLayout->addWidget(CloseBtn);
connect(CloseBtn, SIGNAL(clicked()), this, SLOT(CloseBtnClicked()));
port = 5555;
udpSocket = new QUdpSocket(this);
connect(udpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived()));
bool result = udpSocket->bind(port);
if(!result)
{
QMessageBox::information(this, "error", "udp socket create error!");
return;
}
}
UdpClient::~UdpClient()
{
}
void UdpClient::CloseBtnClicked()
{
close();
}
void UdpClient::dataReceived()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size());
ReceiveTextEdit->insertPlainText(datagram.data());
}
}
main.cpp
#include "udpclient.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpClient w;
w.show();
return a.exec();
}