QT实现udp通信
以下是使用QT实现UDP通信的完整源代码,包括发送和接收消息:
// mainwindow.h文件
#include <QMainWindow>
#include <QUdpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_sendButton_clicked();
void on_receiveButton_clicked();
void processPendingDatagrams();
private:
Ui::MainWindow *ui;
QUdpSocket *udpSocket;
};
// mainwindow.cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHostAddress>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpSocket = new QUdpSocket(this);
udpSocket->bind(5555);
connect(udpSocket, SIGNAL(readyRead()), th