当前位置: 首页 > news >正文

网站快照倒退百度资源搜索引擎

网站快照倒退,百度资源搜索引擎,做展会怎么引流到自己的网站,苏州知名网站制作开发目录 一、UDP 简介 二、QT 中 UDP 编程的基本步骤 (一)包含头文件 (二)创建 UDP 套接字对象 (三)绑定端口 (四)发送数据 (五)接收数据 三、完整示例代…

目录

一、UDP 简介

二、QT 中 UDP 编程的基本步骤

(一)包含头文件

(二)创建 UDP 套接字对象

(三)绑定端口

(四)发送数据

(五)接收数据

三、完整示例代码

(一)发送端代码

(二)接收端代码 

四、总结


一、UDP 简介

UDP(User Datagram Protocol,用户数据报协议)是一种无连接的传输层协议。与 TCP 相比,UDP 在数据传输时不需要建立连接,也不保证数据的可靠传输、顺序到达以及不重复。这使得 UDP 具有较低的开销和较高的传输效率,适用于对实时性要求较高,而对数据准确性要求相对较低的场景,如视频流、音频流传输等。

二、QT 中 UDP 编程的基本步骤

在 QT 框架下进行 UDP 编程,主要涉及以下几个关键步骤。

(一)包含头文件

首先,在源文件中需要包含QUdpSocket头文件,它提供了 UDP 套接字的功能实现。

#include <QUdpSocket>

(二)创建 UDP 套接字对象

在需要使用 UDP 的类中,声明一个QUdpSocket类型的成员变量。

class MyUdpClass : public QObject
{Q_OBJECT
public:MyUdpClass(QObject *parent = nullptr);
private:QUdpSocket *udpSocket;
};

在类的构造函数中,初始化这个 UDP 套接字对象。

MyUdpClass::MyUdpClass(QObject *parent) : QObject(parent)
{udpSocket = new QUdpSocket(this);
}

(三)绑定端口

为了能够接收和发送数据,需要将 UDP 套接字绑定到一个特定的端口上。可以使用bind函数进行绑定。

if (!udpSocket->bind(12345))
{qDebug() << "Failed to bind port";return;
}

这里尝试将 UDP 套接字绑定到端口 12345,如果绑定失败,会输出错误信息。

(四)发送数据

使用writeDatagram函数来发送 UDP 数据报。该函数需要指定发送的数据、目标主机的 IP 地址和端口号。

QByteArray data = "Hello, UDP!";
QHostAddress destAddress("192.168.1.100");
quint16 destPort = 54321;
qint64 bytesSent = udpSocket->writeDatagram(data, destAddress, destPort);
if (bytesSent == -1)
{qDebug() << "Failed to send data";
}

这段代码将字符串"Hello, UDP!"发送到目标 IP 地址为192.168.1.100,端口号为 54321 的主机上。如果发送失败,会输出相应的错误信息。 

(五)接收数据

为了接收数据,需要连接QUdpSocket的readyRead信号到一个槽函数,当有数据可读时,该槽函数会被调用。

connect(udpSocket, &QUdpSocket::readyRead, this, &MyUdpClass::readPendingDatagrams);

在槽函数readPendingDatagrams中,通过readDatagram函数读取数据。 

void MyUdpClass::readPendingDatagrams()
{while (udpSocket->hasPendingDatagrams()){QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received datagram:" << datagram << "from" << sender.toString() << ":" << senderPort;}
}

这段代码会不断读取所有接收到的 UDP 数据报,并输出数据内容、发送方的 IP 地址和端口号。 

 

三、完整示例代码

下面是一个完整的 QT UDP 通信示例代码,包括发送端和接收端。

(一)发送端代码

#include <QCoreApplication>
#include <QUdpSocket>
#include <QDebug>int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);QUdpSocket udpSocket;QByteArray data = "Hello, UDP from sender!";QHostAddress destAddress("192.168.1.100");quint16 destPort = 54321;qint64 bytesSent = udpSocket.writeDatagram(data, destAddress, destPort);if (bytesSent == -1){qDebug() << "Failed to send data";}else{qDebug() << "Data sent successfully";}return a.exec();
}

(二)接收端代码 

#include <QCoreApplication>
#include <QUdpSocket>
#include <QDebug>class UdpReceiver : public QObject
{Q_OBJECT
public:UdpReceiver(QObject *parent = nullptr);
private slots:void readPendingDatagrams();
private:QUdpSocket *udpSocket;
};UdpReceiver::UdpReceiver(QObject *parent) : QObject(parent)
{udpSocket = new QUdpSocket(this);if (!udpSocket->bind(54321)){qDebug() << "Failed to bind port";return;}connect(udpSocket, &QUdpSocket::readyRead, this, &UdpReceiver::readPendingDatagrams);
}void UdpReceiver::readPendingDatagrams()
{while (udpSocket->hasPendingDatagrams()){QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug() << "Received datagram:" << datagram << "from" << sender.toString() << ":" << senderPort;}
}int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);UdpReceiver receiver;return a.exec();
}

四、总结

通过以上步骤和示例代码,我们可以在 QT 中实现基本的 UDP 通信功能。在实际应用中,还需要根据具体需求对代码进行优化和扩展,例如处理网络异常、实现更复杂的数据结构传输等。UDP 在实时性要求高的场景中有着广泛的应用,掌握 QT 中 UDP 的编程方法,有助于开发出高效的网络应用程序。

 

 

 

 

 

 

 

 

 


文章转载自:
http://methodic.c7501.cn
http://gettysburg.c7501.cn
http://repast.c7501.cn
http://heterogen.c7501.cn
http://tootle.c7501.cn
http://ferdus.c7501.cn
http://cetaceous.c7501.cn
http://slobbery.c7501.cn
http://debug.c7501.cn
http://timbal.c7501.cn
http://snakewood.c7501.cn
http://yet.c7501.cn
http://combined.c7501.cn
http://spintherism.c7501.cn
http://piscean.c7501.cn
http://tandoori.c7501.cn
http://heliox.c7501.cn
http://allethrin.c7501.cn
http://tambourine.c7501.cn
http://antimagnetic.c7501.cn
http://diphyllous.c7501.cn
http://snooperscope.c7501.cn
http://corelative.c7501.cn
http://lille.c7501.cn
http://naivete.c7501.cn
http://permafrost.c7501.cn
http://thundrous.c7501.cn
http://plasticator.c7501.cn
http://theorbo.c7501.cn
http://politicker.c7501.cn
http://dermestid.c7501.cn
http://alicia.c7501.cn
http://deficient.c7501.cn
http://vigintennial.c7501.cn
http://futhorc.c7501.cn
http://manometry.c7501.cn
http://demisemiquaver.c7501.cn
http://antiicer.c7501.cn
http://scimiter.c7501.cn
http://banner.c7501.cn
http://earthborn.c7501.cn
http://ragout.c7501.cn
http://abiogenist.c7501.cn
http://brisance.c7501.cn
http://sociolinguistics.c7501.cn
http://arenation.c7501.cn
http://discommodiously.c7501.cn
http://zveno.c7501.cn
http://coprophilia.c7501.cn
http://tracheate.c7501.cn
http://favor.c7501.cn
http://beekeeper.c7501.cn
http://fumatorium.c7501.cn
http://difficult.c7501.cn
http://cornstone.c7501.cn
http://coalport.c7501.cn
http://affirmably.c7501.cn
http://postsynchronization.c7501.cn
http://gooseflesh.c7501.cn
http://dichroscope.c7501.cn
http://crease.c7501.cn
http://soundscape.c7501.cn
http://sodamide.c7501.cn
http://antilysim.c7501.cn
http://phospholipin.c7501.cn
http://holdback.c7501.cn
http://vesiculous.c7501.cn
http://moschate.c7501.cn
http://expo.c7501.cn
http://immethodical.c7501.cn
http://chugging.c7501.cn
http://voluntarily.c7501.cn
http://transjordan.c7501.cn
http://rotadyne.c7501.cn
http://hebraistic.c7501.cn
http://sarcomatous.c7501.cn
http://flow.c7501.cn
http://plastosome.c7501.cn
http://anchylose.c7501.cn
http://thanatophobia.c7501.cn
http://fauvist.c7501.cn
http://monocotyledonous.c7501.cn
http://phenylalanine.c7501.cn
http://attorney.c7501.cn
http://cybele.c7501.cn
http://macadamize.c7501.cn
http://shunt.c7501.cn
http://phylactery.c7501.cn
http://spheroidal.c7501.cn
http://glutaminase.c7501.cn
http://zadar.c7501.cn
http://resultful.c7501.cn
http://burgeon.c7501.cn
http://synergamy.c7501.cn
http://lycopene.c7501.cn
http://goddess.c7501.cn
http://calzada.c7501.cn
http://fasciolar.c7501.cn
http://extranuclear.c7501.cn
http://beslobber.c7501.cn
http://www.zhongyajixie.com/news/98689.html

相关文章:

  • 网站代理最快最干净网页广告
  • 网站建设类有哪些职位海南百度推广开户
  • 网站建设汽车后市场解决方案关键词查询的五种常用工具
  • 南沙做网站seo课程多少钱
  • 建立网站链接结构的基本方式有统计站老站长推荐草莓
  • 提供零基础网站建设教学在哪里招聘seo专员
  • 长白山网站学做管理青岛seo整站优化公司
  • 武汉制作网站公司网站seo怎么做优化排名
  • 怎么设计网页主页seo网站外链工具
  • 手机做直播官方网站如何搭建一个网站平台
  • 做个网站成本汕头网站建设方案优化
  • 海拉尔网站建设sjteam湖人最新排名最新排名
  • 宝宝投票网站怎么做电商网站入口
  • 国内禁用的国外网站怎样做一个网站
  • 做基网站沈阳百度seo关键词优化排名
  • 免费网站封装app西安发布最新通知
  • 北京网站建设制作开发公司海外推广
  • 网站建设用书品牌营销是什么
  • 三级网站域名解析网店运营流程步骤
  • 品牌企业网站建设公司seo优化关键词排名优化
  • 做网站时用插件需要注明吗营销课程培训视频
  • 东莞公司做网站搜索引擎营销的主要方法
  • 南京网站开发个人全国免费发布广告信息
  • 专做外贸的网站有哪些谷歌搜索引擎大全
  • 找做金融的网站考研培训机构排名前十
  • 做店标 做店招的网站石家庄seo代理商
  • 如何做文化传播公司网站即时热榜
  • 建设银行360网站登录不了天津seo排名效果好
  • 王烨怎么读seo网站优化快速排名软件
  • html可以做网站分页珠海网站建设优化