UDP服务端与客户端
编写一个 UDP
服务器 和 UDP
客户端。实现服务端不断广播信息,客户端接收信息,并将信息显示在客户端的文本框内。
实现效果
代码实现
1. 服务端 UdpServer
udpserver.h
:
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QTimer>
#include <QUdpSocket>
class UdpServer : public QDialog
{
Q_OBJECT
public:
UdpServer(QWidget *parent = nullptr);
~UdpServer();
public slots:
void startBtnClicked();
void timeout();
private:
QLabel * timeLabel;
QLineEdit * timeLineEdit;
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)
: QDialog(parent)
{
setWindowTitle(tr("Udp Server"));
timeLabel = new QLabel(tr("计时器:"));
timeLineEdit = new QLineEdit;
startBtn = new QPushButton(tr("开始"));
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(timeLabel);
mainLayout->addWidget(timeLineEdit);
mainLayout->addWidget(startBtn);
connect(startBtn,SIGNAL(clicked()),this,SLOT(startBtnClicked()));
port = 5555;
isStarted = false;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
}
void UdpServer::startBtnClicked(){
if(!isStarted){
startBtn->setText(tr("停止"));
timer->start(1000);
isStarted = true;
}
else{
startBtn->setText(tr("开始"));
timer->stop();
isStarted = false;
}
}
void UdpServer::timeout(){
QString msg = timeLineEdit->text();
if(msg == ""){
return;
}
if(udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port) != msg.length()){
return;
}
}
UdpServer::~UdpServer()
{
}
main.cpp
:
#include "udpserver.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpServer w;
w.show();
return a.exec();
}
2. 客户端 UdpClient
udpclient.h
:
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QDialog>
#include <QTextEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
class UdpClient : public QDialog
{
Q_OBJECT
public:
UdpClient(QWidget *parent = nullptr);
~UdpClient();
public slots:
void closeBtnClicked();
void dataReceived();
private:
QTextEdit * receiveTextEidt;
QPushButton * closeBtn;
QVBoxLayout * mainLayout;
int port;
QUdpSocket * udpSocket;
};
#endif // UDPCLIENT_H
udpclient.cpp
:
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("Udp Client"));
receiveTextEidt = new QTextEdit;
closeBtn = new QPushButton(tr("Close"));
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(receiveTextEidt);
mainLayout->addWidget(closeBtn);
connect(closeBtn,SIGNAL(clicked()),this,SLOT(closeBtnClicked()));
port = 5555;
udpSocket = new QUdpSocket(this);
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
bool flag = udpSocket->bind(port);
if(!flag){
QMessageBox::warning(this,tr("error"),tr("UdpSocket create error!"));
return;
}
}
void UdpClient::closeBtnClicked(){
close();
}
void UdpClient::dataReceived(){
while(udpSocket->hasPendingDatagrams()){
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString msg = datagram.data();
receiveTextEidt->insertPlainText(msg);
}
}
UdpClient::~UdpClient()
{
}
main.cpp
:
#include "udpclient.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UdpClient w;
w.show();
return a.exec();
}
3. 设置允许运行多个项目
最后分别运行 服务端 和 客户端即可。