QT网络与通信:UDP客户端和服务端小实例
程序员文章站
2022-07-01 15:02:08
...
在.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();
private:
QLabel *TimeLabel;
QLineEdit *TextLineEdit;
QPushButton *StartBtn;
QVBoxLayout *mainLayout;
int port;
bool isStarted;
QUdpSocket *udpSocket;
QTimer *timer;
private slots:
void StartBtnClicked();
void timeout();
};
#endif // UDPSERVER_H
在udpserver.cpp中添加
#include "udpserver.h"
#include <QHostAddress>
udpserver::udpserver(QWidget *parent,Qt::WindowFlags f)//构造函数
: QDialog(parent,f)
{
resize(200,150);
setWindowTitle(tr("UDP Server"));
TimeLabel =new QLabel(tr("计时器"),this);
TextLineEdit =new QLineEdit(this);
StartBtn =new QPushButton(tr("开始"));
mainLayout =new QVBoxLayout(this);
mainLayout->addWidget(TimeLabel);
mainLayout->addWidget(TextLineEdit);
mainLayout->addWidget(StartBtn);
connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));
port=5555;//设置UDP的端口号参数,服务器定时向此端口发送广播参数
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);//开始启动计时器并执行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 ;
} //发送数据报到相应的端口
//writeDatagram返回结果是发送成功的字节数
}
udpserver::~udpserver()
{
}
客户端:
在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();
private:
QTextEdit *receiveTextEdit;
QVBoxLayout *mainLayout;
QPushButton *closeBtn;
private slots:
void CloseBtnClicked();
void dataReceived();
private :
int port;
QUdpSocket *udpsocket;//操作系统和程序直接的连接
};
#endif // UDPCLIENT_H
在udpclient.cpp中添加
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
udpclient::udpclient(QWidget *parent,Qt::WindowFlags f)
: QDialog(parent,f)
{
setWindowTitle(tr("客户端"));
receiveTextEdit = new QTextEdit(this);
closeBtn = new QPushButton(tr("关闭"),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()));
//readyRead()当有数据到达设备时,就触发这个信号
bool result =udpsocket->bind(port);
//用于未连接的数据报或流类套接口,把一本地地址与一套接口捆绑,在connect()或listen()调用前使用。
if(!result)
{
QMessageBox::information(this,tr("error"),tr("udp socket create error!"));
return ;
}
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();
receiveTextEdit->insertPlainText(msg);//显示数据内容
}
}
udpclient::~udpclient()
{
}
具体函数已有注释!!!