一、知识点总结
udpSocket->bind(8888); //绑定端口
connect(udpSocket,&QUdpSocket::readyRead,this,&UdpA::dealMsg); //收到对方的消息,立即触发
char buf[1024] = {0};//缓存
QHostAddress peerIp; //对方IP
quint16 peerPort; //对方端口
qint16 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);// 读到多少个字符
QString("[%1:%2] %3").arg(peerIp.toString()).arg(peerPort).arg(buf); //字符串模板占位
QString ip = ui->ip->text();
qint16 port = ui->port->text().toInt(); //端口号
QString msg = ui->msg->toPlainText(); //输入框内容
udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);// 写出字符串
二、案例
1、UdpA
UdpA.h
#ifndef UDPA_H
#define UDPA_H
#include <QWidget>
#include <QUdpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class UdpA; }
QT_END_NAMESPACE
class UdpA : public QWidget
{
Q_OBJECT
public:
UdpA(QWidget *parent = nullptr);
~UdpA();
void dealMsg(); //槽函数,处理对方发过来的消息
private slots:
void on_pushButton_clicked();
private:
Ui::UdpA *ui;
QUdpSocket * udpSocket;
};
#endif // UDPA_H
UdpA.cpp
#include "UdpA.h"
#include "ui_UdpA.h"
UdpA::UdpA(QWidget *parent)
: QWidget(parent)
, ui(new Ui::UdpA)
{
ui->setupUi(this);
setWindowTitle("UdpA:8888");
//分配空间,指定父对象,自动释放内存
udpSocket = new QUdpSocket(this);
//绑定端口
udpSocket->bind(8888);
//收到对方的消息,立即触发
connect(udpSocket,&QUdpSocket::readyRead,this,&UdpA::dealMsg);
}
UdpA::~UdpA()
{
delete ui;
}
//处理对方发过来的内容
void UdpA::dealMsg()
{
char buf[1024] = {0}; //缓存
QHostAddress peerIp; //对方IP
quint16 peerPort; //对方端口
qint16 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);//读到多少个字符
if(length>0){
QString msgTxt = QString("[%1:%2] %3")
.arg(peerIp.toString())
.arg(peerPort)
.arg(buf); //字符串模板占位
ui->msg->setText(msgTxt);
}
}
void UdpA::on_pushButton_clicked()
{
//获取对方的IP和端口
QString ip = ui->ip->text();
qint16 port = ui->port->text().toInt();
QString msg = ui->msg->toPlainText();
udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);
}
2、UdpB
UdpB.h
#ifndef UDPB_H
#define UDPB_H
#include <QWidget>
#include <QUdpSocket>
namespace Ui {
class UdpB;
}
class UdpB : public QWidget
{
Q_OBJECT
public:
explicit UdpB(QWidget *parent = nullptr);
~UdpB();
private slots:
void on_pushButton_clicked();
void dealMsg();
private:
Ui::UdpB *ui;
QUdpSocket * udpSocket;
};
#endif // UDPB_H
UdpB.cpp
#include "UdpB.h"
#include "ui_UdpB.h"
UdpB::UdpB(QWidget *parent) :
QWidget(parent),
ui(new Ui::UdpB)
{
ui->setupUi(this);
setWindowTitle("UdpB:9999");
udpSocket = new QUdpSocket(this);
udpSocket->bind(9999);
connect(udpSocket,&QUdpSocket::readyRead,this,&UdpB::dealMsg);
}
UdpB::~UdpB()
{
delete ui;
}
void UdpB::dealMsg(){
QHostAddress peerIp ;
quint16 peerPort ;
char buf[1024] = {0};
qint64 length = udpSocket->readDatagram(buf,sizeof(buf),&peerIp,&peerPort);
if(length > 0){
QString msgTxt = QString("[%1:%2] %3")
.arg(peerIp.toString())
.arg(peerPort)
.arg(buf);
ui->msg->setText(msgTxt);
}
}
void UdpB::on_pushButton_clicked()
{
//获取对方的IP和端口
QString ip = ui->ip->text();
qint16 port = ui->port->text().toInt();
QString msg = ui->msg->toPlainText();
udpSocket->writeDatagram(msg.toUtf8().data(),QHostAddress(ip),port);
}