實(shí)現(xiàn)效果
實(shí)現(xiàn)過(guò)程
準(zhǔn)備工作
在.pro中增加一句話
QT +=network
繪制界面
.h文件內(nèi)容:
#ifndef WIDGET_H
#define WIDGET_H
#include "receivethread.h"
#include "sendthread.h"
#include <QWidget>
#include <QUdpSocket>
#include <iostream>
#include <stdint.h>
#include <sstream>
using namespace std;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
public slots:
//接收信號(hào)
void receiveSlot();
//停止線程
void quitThreaSlot();
private slots:
void on_pushButton_clicked();
void on_pushButton_5_clicked();
void on_pushButton_4_clicked();
private:
Ui::Widget *ui;
receiveThread*preceiveTHread;
private:
QUdpSocket *m_udpSocket = nullptr;
};
#endif // WIDGET_H
構(gòu)造函數(shù)內(nèi)容
ui->setupUi(this);
preceiveTHread = new receiveThread(this);
//本地主機(jī)名
QString hostName = QHostInfo::localHostName();
//本機(jī)IP地址
QHostInfo hostInfo = QHostInfo::fromName(hostName);
//IP地址列表
QList<QHostAddress> addrList = hostInfo.addresses();
for(int i=0;i<addrList.count();i++)
{
QHostAddress host = addrList.at(i);
if(QAbstractSocket::IPv4Protocol == host.protocol())
{
QString ip = host.toString();
ui->comboBox->addItem(ip);
}
}
m_udpSocket = new QUdpSocket(this);
//連接信號(hào)和槽(綁定端口就開始接收信號(hào)線程,接收到數(shù)據(jù)就顯示,關(guān)閉窗口關(guān)閉線程)
connect(this,SIGNAL(destroyed()),this,SLOT(quitThreaSlot()));
connect(preceiveTHread,SIGNAL(receiveSignal()),this,SLOT(receiveSlot()));
對(duì)于綁定按鈕的定義函數(shù):
void Widget::on_pushButton_clicked()
{
//本機(jī)UDP端口
qint16 port = ui->spinBox->value();
if(m_udpSocket->bind(port))
{
ui->plainTextEdit->appendPlainText("**已成功綁定");
ui->plainTextEdit->appendPlainText("**綁定端口: "+QString::number(m_udpSocket->localPort()));
ui->pushButton->setEnabled(false);
}
else
{
ui->plainTextEdit->appendPlainText("**綁定失敗");
}
//啟動(dòng)線程
preceiveTHread->start();
}
接收信號(hào)的槽函數(shù)(UDP接收到數(shù)據(jù)顯示)
void Widget::receiveSlot()
{
ui->lineEdit->clear();
//是否還有待讀取的傳入數(shù)據(jù)報(bào)
while(m_udpSocket->hasPendingDatagrams())
{
QByteArray data;
//返回待讀取的數(shù)據(jù)報(bào)的字節(jié)數(shù)
data.resize(m_udpSocket->pendingDatagramSize());
QHostAddress peerAddr;
quint16 peerPort;
//讀取數(shù)據(jù)報(bào)的內(nèi)容
m_udpSocket->readDatagram(data.data(),data.size(),&peerAddr,&peerPort);
QString str = data.data();
QString peer = "[From ] +"+peerAddr.toString()+":"+QString::number(peerPort)+"] ";
ui->lineEdit->setText(peer+str);
}
}
quitThreaSlot函數(shù):
void Widget::quitThreaSlot()
{
preceiveTHread->quit();
preceiveTHread->wait();
}
退出按鈕定義:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-831272.html
void Widget::on_pushButton_4_clicked()
{
preceiveTHread->terminate();
}
使用的receivethread.h就是將run函數(shù)重寫(循環(huán)發(fā)送定義的信號(hào)延遲即可),在定義一個(gè)信號(hào)即可。
以上即功能的所有有錯(cuò)還請(qǐng)指出文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-831272.html
到了這里,關(guān)于Qt進(jìn)行UDP通訊,創(chuàng)建一個(gè)收線程這樣可以進(jìn)行接收數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!