一、效果展示
Udp网络通信,需要在.pro文件中添加QT += network
二、UdpServer
.h
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UdpServer : public QWidget
{
Q_OBJECT
public:
UdpServer(QWidget *parent = 0,Qt::WindowFlags f=0);
~UdpServer();
private 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
.cpp
#include "udpserver.h"
#include <QHostAddress>
UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f)
: QWidget(parent,f)
{
setWindowTitle("UdpServer");
TimerLabel = new QLabel("计时器",this);
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(bool)),this,SLOT(startBtnClicked()));
port = 5555;
isStarted=false;
udpSocket = new QUdpSocket;
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("开始"));
isStarted=false;
timer->stop();
}
}
void UdpServer::timeout()
{
QString str = textLineEdit->text();
int length=0;
if(str=="")
{
return;
}
length==udpSocket->writeDatagram(str.toLatin1(),str.length(),QHostAddress::Broadcast,port);
if(length!=str.length())
{
return;
}
}
UdpServer::~UdpServer()
{
}
三、udpClient
.h’
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QWidget>
#include <QUdpSocket>
#include <QTextEdit>
#include <QVBoxLayout>
#include <QPushButton>
class UdpClient : public QWidget
{
Q_OBJECT
public:
UdpClient(QWidget *parent = 0,Qt::WindowFlags f=0);
~UdpClient();
private slots:
void closeBtnClicked();
void dataReceive();
private:
int port;
QUdpSocket* udpSocket;
QTextEdit* receiveTextEdit;
QPushButton* closeBtn;
QVBoxLayout* mainLayout;
};
#endif // UDPCLIENT_H
.cpp
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f)
: QWidget(parent,f)
{
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(dataReceive()));
bool result = udpSocket->bind(port);
if(!result)
{
QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
return;
}
}
void UdpClient::closeBtnClicked()
{
close();
}
void UdpClient::dataReceive()
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QString msg = datagram.data();
receiveTextEdit->insertPlainText(msg);
}
}
UdpClient::~UdpClient()
{
}
仅作为个人学习记录