目录
效果展示
实现过程
Qt网络编程---UDP通信
工程中添加 QT += network
QUdpSocket
-
数据发送端
1. 创建QUdpSocket对象
QUdpSocket msocket
2. 调用对象方法发送数据(ip , 端口号)
头文件
#ifndef UDPSEND_H
#define UDPSEND_H
#include <QMainWindow>
#include <QUdpSocket>
namespace Ui {
class UdpSend;
}
class UdpSend : public QMainWindow
{
Q_OBJECT
public:
explicit UdpSend(QWidget *parent = nullptr);
~UdpSend();
private slots:
void on_sendBt_clicked();
void on_sendSigRb_clicked();
void on_sendMulRb_clicked();
void on_sendBroadRb_clicked();
void on_textEdit_cursorPositionChanged();
void on_clearBt_clicked();
private:
Ui::UdpSend *ui;
QUdpSocket *mSocket;
QHostAddress sendaddrees;
QString sendPort;
};
#endif // UDPSEND_H
cpp文件
#include "udpsend.h"
#include "ui_udpsend.h"
#include <QMessageBox>
UdpSend::UdpSend(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UdpSend)
{
ui->setupUi(this);
//初始化创建QUdpSocket对象
mSocket = new QUdpSocket();
ui->sendBt->setEnabled(false);
}
UdpSend::~UdpSend()
{
delete ui;
}
void UdpSend::on_sendBt_clicked()
{
//单播"192.168.12.206"
//组播ip地址范围:224.0.0.0-239.255.255.255
//广播QHostAddress::Broadcast
mSocket->writeDatagram(ui->textEdit->toPlainText().toUtf8(),sendaddrees,sendPort.toInt());
}
//单播
void UdpSend::on_sendSigRb_clicked()
{
if(ui->lineEdit_port->text().isEmpty() || ui->lineEdit_SigAddr->text().isEmpty())
{
QMessageBox::warning(this,"提示","请输入单播ip和端口号");
return;
}
sendaddrees.setAddress( ui->lineEdit_SigAddr->text());
sendPort = ui->lineEdit_port->text();
}
//组播
void UdpSend::on_sendMulRb_clicked()
{
if(ui->lineEdit_port->text().isEmpty() || ui->lineEdit_MulAddr->text().isEmpty())
{
QMessageBox::warning(this,"提示","请输入组播ip和端口号");
return;
}
sendaddrees.setAddress( ui->lineEdit_port->text());
sendPort = ui->lineEdit_MulAddr->text();
}
void UdpSend::on_sendBroadRb_clicked()
{
if(ui->lineEdit_port->text().isEmpty() || ui->lineEdit_BroadAddr->text().isEmpty())
{
QMessageBox::warning(this,"提示","请输入广播ip和端口号");
return;
}
sendaddrees.setAddress( ui->lineEdit_BroadAddr->text());
sendPort = ui->lineEdit_MulAddr->text();
}
void UdpSend::on_textEdit_cursorPositionChanged()
{
if(ui->textEdit->toPlainText().isEmpty())
{
ui->sendBt->setEnabled(false);
}
else
{
ui->sendBt->setEnabled(true);
}
}
void UdpSend::on_clearBt_clicked()
{
ui->textEdit->clear();
}
-
数据接收端
1.创建QUdpSocket对象
2.绑定端口地址
3.当有数据达到的时候会发送readyRead信号
4.在槽函数中调用对象方法读取数据
头文件
#ifndef UDPRECV_H
#define UDPRECV_H
#include <QMainWindow>
#include <QUdpSocket>
namespace Ui {
class UdpRecv;
}
class UdpRecv : public QMainWindow
{
Q_OBJECT
public:
explicit UdpRecv(QWidget *parent = nullptr);
~UdpRecv();
private slots:
void read_data();
void on_checkBox_clicked(bool checked);
void on_recvCb_clicked(bool checked);
void on_recvJoinMulBt_clicked();
void on_clearBt_clicked();
void on_exitBt_clicked();
private:
Ui::UdpRecv *ui;
QUdpSocket *mSocket;
};
#endif // UDPRECV_H
cpp文件
#include "udprecv.h"
#include "ui_udprecv.h"
#include <QMessageBox>
UdpRecv::UdpRecv(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::UdpRecv)
{
ui->setupUi(this);
mSocket = new QUdpSocket();
//绑定
mSocket->bind(QHostAddress::AnyIPv4,6677);
//关联读数据信号readyread
connect(mSocket,SIGNAL(readyRead()),this,SLOT(read_data()));
}
UdpRecv::~UdpRecv()
{
delete ui;
}
void UdpRecv::read_data()
{
QByteArray array;
QHostAddress address;
quint16 port;
array.resize(mSocket->bytesAvailable());//根据可读数据来设置空间大小
mSocket->readDatagram(array.data(),array.size(),&address,&port); //读取数据
ui->listWidget->addItem(array);//显示数据
}
//选择接收
void UdpRecv::on_recvCb_clicked(bool checked)
{
if(ui->lineEdit_port->text().isEmpty())
{
QMessageBox::warning(this,"提示","请输入端口号");
ui->recvCb->setChecked(false);
return;
}
if(checked)
{
mSocket->bind(QHostAddress::AnyIPv4,ui->lineEdit_port->text().toInt());
connect(mSocket,SIGNAL(readyRead()),this,SLOT(read_data()));
ui->lineEdit_port->setEnabled(false);
}
else
{
mSocket->close();
ui->lineEdit_port->setEnabled(true);
}
}
void UdpRecv::on_recvJoinMulBt_clicked()
{
if(ui->lineEdit_mulAddr->text().isEmpty())
{
QMessageBox::warning(this,"提示","请输入组播ip");
return;
}
if(mSocket->joinMulticastGroup(QHostAddress(ui->lineEdit_mulAddr->text()))) //加入组播
{
ui->recvMulAddr->addItem(ui->lineEdit_mulAddr->text());
}
else
{
QMessageBox::warning(this,"提示","加入组播失败,请修改ip后继续加入");
//return;
}
}
void UdpRecv::on_clearBt_clicked()
{
ui->listWidget->clear();
}
void UdpRecv::on_exitBt_clicked()
{
mSocket->leaveMulticastGroup(QHostAddress(ui->recvMulAddr->currentIndex()));//退出组播地址列表当前的组播
ui->recvMulAddr->removeItem(ui->recvMulAddr->currentIndex()); //删除组播地址列表中当前的组播
}