UDP通信示例图
下图为UDP通信的简单界面,能是实现绑定本地IP、端口号和指定目标IP和端口号以及接收、发送数据功能
UDP使用前的准备
- 在pro文件中添加 “QT += network”(需要添加后才能使用网络通信)
- UDP发送可以通过writeDatagram函数(需要指定目标ip和端口号)
- UDP读取可以通过readDatagram函数(需要接收数据的char *、接收数据的长度以及能保存目标ip和端口号的变量指针)
- UDP收到新信息时会发出readyRead信号
- UDP通信需要保存目标端的IP和端口号(方便通信)
自定义UDP通信类的两种方法
- 类继承对应的UDP类,通过this指针实现UDP通信操作
- 类中包含对应的UDP类对象,通过该对象实现UDP通信操作(本文使用该方法)
源码(含注释)
自定义UDP类
CUdpSocket.h
#ifndef CUDPSOCKET_H
#define CUDPSOCKET_H
#include <QObject>
#include <QUdpSocket>
#include <QHostAddress>
class CUdpSocket : public QObject
{
Q_OBJECT
public:
explicit CUdpSocket(QObject *parent = nullptr);
~CUdpSocket();
void bind(QString ip, int port);
void sendData(QString data);
void setTargetInfo(QString ip, int port);
signals:
void recvDataSignal(QString data);
public slots:
void readyReadData();
private:
QUdpSocket *m_sock;
QHostAddress m_hostAddr;
quint16 m_port;
};
#endif
CUdpSocket.cpp
#include "CUdpSocket.h"
CUdpSocket::CUdpSocket(QObject *parent) : QObject(parent)
{
m_sock = new QUdpSocket;
connect(m_sock, &QUdpSocket::readyRead, this, &CUdpSocket::readyReadData);
}
CUdpSocket::~CUdpSocket()
{
delete m_sock;
}
void CUdpSocket::bind(QString ip, int port)
{
m_sock->bind(QHostAddress(ip), port);
}
void CUdpSocket::sendData(QString data)
{
m_sock->writeDatagram(data.toUtf8(), m_hostAddr, m_port);
}
void CUdpSocket::setTargetInfo(QString ip, int port)
{
m_hostAddr = QHostAddress(ip);
m_port = port;
}
void CUdpSocket::readyReadData()
{
while(m_sock->hasPendingDatagrams())
{
QByteArray data;
data.resize(m_sock->pendingDatagramSize());
m_sock->readDatagram(data.data(), data.size(), &m_hostAddr, &m_port);
emit recvDataSignal(data);
}
}
CMainWindow类(自定义TCP通信类的调用)
CMainWindow.h
#ifndef CMAINWINDOW_H
#define CMAINWINDOW_H
#include <QMainWindow>
#include "CUdpSocket.h"
namespace Ui {
class CMainWindow;
}
class CMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit CMainWindow(QWidget *parent = 0);
~CMainWindow();
private slots:
void on_bindBtn_clicked();
void on_sendBtn_clicked();
void on_appendData(QString data);
private:
Ui::CMainWindow *ui;
CUdpSocket *m_udpSock;
};
#endif
CMainWindow.cpp
#include "CMainWindow.h"
#include "ui_CMainWindow.h"
CMainWindow::CMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::CMainWindow)
{
ui->setupUi(this);
m_udpSock = new CUdpSocket;
connect(m_udpSock, &CUdpSocket::recvDataSignal, this, &CMainWindow::on_appendData);
}
CMainWindow::~CMainWindow()
{
delete m_udpSock;
delete ui;
}
void CMainWindow::on_bindBtn_clicked()
{
if("已绑定" == ui->bindBtn->text())
return;
ui->bindBtn->setText("已绑定");
m_udpSock->bind(ui->ipEdit->text(), ui->portEdit->text().toInt());
m_udpSock->setTargetInfo(ui->ipEdit_2->text(), ui->portEdit_2->text().toInt());
}
void CMainWindow::on_sendBtn_clicked()
{
m_udpSock->sendData(ui->textEdit->toPlainText());
}
void CMainWindow::on_appendData(QString data)
{
ui->textBrowser->append(data);
}
总结
UDP需要绑定本地IP和端口号,并且需要指定目标IP和端口号才能发送消息,并且在发送信息和接收信息的时候需要接收和指定目标主机的信息(不过本文只是简单的UDP通信功能,有兴趣可以补全UDP的其他功能)
相关文档
自定义TCP类
注:本文仅为作者编程过程中所遇到的问题和总结,并非代表绝对正确,若有错误欢迎指出。 注:如有侵权,联系作者删除
|