效果图
服务端源码
注意:在.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;//发送内容的lineedit
QPushButton *StartBtn;//开始按钮
QVBoxLayout *mainLayout;//布局
int port;//端口号
bool isStarted;//是否开始
QUdpSocket *udpSocket;//udp
QTimer *timer;//计时器
};
#endif // UDPSERVER_H
udpserver.cpp
#include "udpserver.h"
#include <QHostAddress>
UdpServer::UdpServer(QWidget *parent, Qt::WindowFlags f)
: QDialog(parent,f)
{
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;
isStarted=false;
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秒执行一次timeout()
isStarted =true;
}
else
{
StartBtn->setText(tr("开始"));
isStarted = false;
timer->stop();//停止
}
}
void UdpServer::timeout()
{
QString msg = TextLineEdit->text();
int length=0;
if(msg=="")
{
return;
}
//通过udp写入传输数据
if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length())
{
return;
}
}
客户端源码
注意:在.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;//udp
};
#endif // UDPCLIENT_H
udpclient.cpp
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f)
: QDialog(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(dataReceived()));
bool result=udpSocket->bind(port);
//当绑定失败时弹出提示
if(!result)
{
QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
return;
}
}
UdpClient::~UdpClient()
{
}
void UdpClient::CloseBtnClicked()
{
close();
}
void UdpClient::dataReceived()
{
//通过udp接收数据
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
//读取数据
udpSocket->readDatagram(datagram.data(),datagram.size());
QString msg=datagram.data();
//向textdit中插入数据
ReceiveTextEdit->insertPlainText(msg);
}
}