程序
需要在.pro中加入网络模块QT+=network
该程序在服务器设置了一个定时器,周期性的向客户端发送信息。
服务器
.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);
~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
.cpp
#include "udpserver.h"
#include <QHostAddress>
#include <QUdpSocket>
#include <QDebug>
UdpServer::UdpServer(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("UDP Server"));
/* 初始化各个控件 */
TimerLabel = new QLabel(tr("计时器:"),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()),this,SLOT(StartBtnClicked()));
port = 5555; //设置UDP的端口号参数
isStarted = false;
udpSocket=new QUdpSocket;//创建一个QUdpSocket
timer = new QTimer(this);
//定时发送广播信息
connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));
}
UdpServer::~UdpServer()
{
}
void UdpServer::StartBtnClicked()
{
if(!isStarted)
{
StartBtn->setText("停止");
timer->start(1000);
isStarted=true;
}
else
{
StartBtn->setText("开始");
isStarted=false;
timer->stop();
}
}
void UdpServer::timeout()
{
QString msg =TextLineEdit->text();
qDebug()<<msg.toLatin1();
int length=0;
if(msg=="")
{
return;
}
if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length())//QHostAddress::Broadcast向广播地址发送
{
return;
}
}
客户端
.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);
~UdpClient();
public slots:
void CloseBtnClicked();
void dataReceived();
private:
QTextEdit *ReceiveTextEdit;
QPushButton *CloseBtn;
QVBoxLayout *mainLayout;
int port;
QUdpSocket *udpSocket;
};
#endif // UDPCLIENT_H
.cpp
#include "udpclient.h"
#include <QUdpSocket>
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent)
: 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; //设置UDP的端口号参数,指定在此端口上监听数据
udpSocket = new QUdpSocket(this); //创建一个QUdpSocket
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());
QString msg=datagram.data();
ReceiveTextEdit->insertPlainText(msg);
}
}