




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
qt4使用QUdpSocket发送数据报datagrams2013-07-26 | 分类:QT| 标签: | 浏览(0)UDP服户端绑定广播喝小酒的网摘/a/11512.htmudpSocket = new QUdpSocket(this);QByteArray datagram = ;udpSocket-writeDatagram(datagram.data(), datagram.size(), QHostAddress:Broadcast, 45454);udpSocket-bind(QHostAddress(04), 45454);UDP客户端广播QUdpSocket *udpsocket1 = new QUdpSocket(this);QByteArray datagram = ; udpsocket1-writeDatagram(datagram.data(),datagram.size(), QHostAddress:Broadcast,ui-spinBox-text().toInt();UDP客户端发送到指定IPQUdpSocket *udpsocket1 = new QUdpSocket(this);QByteArray datagram = ; udpsocket1-writeDatagram(datagram.data(),datagram.size(), QHostAddress(04),ui-spinBox-text().toInt();收数据connect(udpsocket, SIGNAL(readyRead(), this, SLOT(readPendingDatagrams();while (udpsocket-hasPendingDatagrams()QByteArray datagram;datagram.resize(udpsocket-pendingDatagramSize();QHostAddress sender;quint16 senderPort;udpsocket-readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);qDebug()datagram.data()datagram.size()senderPortwriteDatagram(datagram.data(),datagram.size(),QHostAddress(00),45454);6. /sender-writeDatagram(datagram.data(),datagram.size(),QHostAddress:Broadcast,45454);7. /sender-writeDatagram(datagram.data(),datagram.size(),QHostAddress:LocalHost,45454);8. deletesender;9. 接收(服务器端监听)使用QUdpSocket 的bind函数监听某个端口当监听的端口有数据到达时,QUdpSocket 的信号readyRead()就emit,然后在对应的槽函数里使用QUdpSocket 的readDatagram读取数据void QIODevice:readyRead ()signalThis signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.readyRead() is not emitted recursively; if you reenter the event loop or callwaitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (althoughwaitForReadyRead() may still return true).Note for developers implementing classes derived fromQIODevice: you should always emit readyRead() when new data has arrived (do not emit it only because theres data still to be read in your buffers). Do not emit readyRead() in other conditions.cppview plaincopy1. private:2. QUdpSocket*receiver;3. privateslots:4. voidprocessPendingDatagram();cppview plaincopy1. receiver=newQUdpSocket(this);2. receiver-bind(45454,QUdpSocket:ShareAddress);3. connect(receiver,SIGNAL(readyRead(),this,SLOT(processPendingDatagram();cppview plaincopy1. voidWidget:processPendingDatagram()/处理等待的数据报2. 3. while(receiver-hasPendingDatagrams()/拥有等待的数据报4. 5. QByteArraydatagram;/拥于存放接收的数据报6. datagram.resize(receiver-pendingDatagramSize();7. /让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据8. receiver-readDatagram(datagram.data(),datagram.size();9. /接收数据报,将其存放到datagram中10. ui-label-setText(datagram);11. /将数据报内容显示出来12. 13. 用wireshark监听xp 00和虚拟机fedora 03之间的udp数据包,如下 hello worldxp 00-fedora 03(wireshark操作:capture-Options里选择要监视的网卡,然后点Start。可以选择capture/capture filters然后选择udp only过滤一下)嗯,这个QImage的问题研究好久了,有段时间没用,忘了,已经被两次问到了,突然有点解释不清楚,我汗颜,觉得有必要重新总结下了,不然无颜对自己了。图像的数据是以字节为单位保存的,每一行的字节数必须是4的整数倍,不足的补0。(因为我们使用的是32操作系统,因此数据是按照32位对齐的,所以每行的字节数必须是4的整数倍也就是说每行的数据位必须是32位的整数倍。)这里是按照我的理解的,貌似错了,修正一下,最近在看数据对齐,这段话先忽略了,没有删掉,是因为,想留个足迹,等我找到合适的答案再贴上来。不过,图像的数据确实是按32位对齐的。如果不是整数倍,则根据公式: W = ( w * bitcount + 31 )/32 * 4;注: w是图像的宽度,bitcount是图像的位深,即32、24等, 计算得到的W是程序中图像每行的字节数。这里讲述QImage的32、24、8位图。图像格式:QImage:Format_RGB32 ,QImage:Format_RGB888,QImage:Format_Indexed8。构造图像: (1)、QImage myImage1 = QImage(filename); 根据文件名打开图像,如果图像本身是32、24位的,程序中图像是32位的,如果图像本身是8位、1位的,程序中对应为8位、1位。 (2)、QImage myImage2 = QImage(width, height, QImage:Format_); 根据图像宽高来构造一幅图像,程序会自动根据图像格式对齐图像数据。操作图像:按照(2)的方式构造图像,在Debug下,如果不给图像myImage2初值,图像不是黑的, 但release下,则构造好的图像默认为黑色。好了,现在我们需要对图像数据操作,32位图像无疑是最简单的,因为它数据是对齐的。用width表示图像宽度,height表示图像高度。首先熟悉几个函数:a、uchar* bits(); 可以获取图像的首地址b、int byteCount(); 图像的总字节数c、int bytesPerLine(); 图像每行字节数1、QImage:Format_RGB32,存入格式为B,G,R,A 对应 0,1,2,3 QImage:Format_RGB888,存入格式为R, G, B 对应 0,1,2 QImage:Format_Indexed8,需要设定颜色表,QVector 灰度图像颜色表设定: QVector colorTable; for(int k=0;k256;+k) colorTable.push_back( qRgb(k,k,k) ); 2、QImage image32 = QImage(width, height, QImage:Format_32); QImage image24 = QImage(width, height, QImage:Format_24); QImage image8 = QImage(width, height, QImage:Format_8); image8.setColorTable(colorTable);3、需要取每个像素处理,采用指针取值,行扫的方式: int lineNum_32 = 0; /行数 int pixelsub_32 = 0; /像素下标 uchar* imagebits_32 = image32.bits(); /获取图像首地址,32位图 uchar* imagebits24 = image24.bits(); uchar* imagebits8 = image8.bits(); for(int i=0; iheight; +i) /按照通常的理解,我们会如下处理,取每行 lineNum_32 = i * width * 4; /对于任意图像,这句没有问题 / lineNum_24 = i * width * 3; /?当width不是4的整数倍时,这句取不到每行开头 / lineNum_8 = i * width; /?当width不是4的整数倍时,这句取不到每行开头 for(int j=0; jwidth; +j) int r_32 = imagebits_32 lineNum_32 + j * 4 + 2; int g_32 = imagebits_32 lineNum_32 + j * 4 + 1; int b_32 = imagebits_32 lineNum _32 + j * 4; / int r_24 = imagebits_24 lineNum_24 + j * 3; /注意区别32位的图 / int g_24 = imagebits_24 lineNum_24 + j *3 + 1; / int b_24 = imagebits_24 lineNum_24 + j * 3 + 2; / int gray_8 = imagebits_8 lineNum_8 + j; /自己的操作 /?出问题了,因为实际的图像数据并不是以width为真实宽度的,解决,有两种方法:第一种方法:自己计算实际的宽度修改为:/ 获取每行的字节数int W_32 = ( width * 32 + 31 )/32 * 4; /注意这里没有四舍五入,所以不要随意换算int W_24 = ( width * 24 + 31 )/32 * 4;int W_8 = ( width * 8 + 31)/32 * 4; /也可以使用QT函数来获取,功能和上面一样 int W_32 = image32.bytesPerLine(); int W_24 = image24.bytesPerLine(); int W_8 = image8.bytesPerLine();for(int i=0; iheight; +i) /现在可以按照通常的理解,取每行 lineNum_32 = i * W_32; /注意,这里不再需要乘倍数了(4, 3等) / lineNum_24 = i * W_24; / lineNum_8 = i * W_8; for(int j=0; jwidth; +j) /这里的操作同上面的一样 第二种方法:采用scanLine(int)来获取每行的首地址,for(int i=0; iheight; +i) imagebits_32 = image32.scanLine(i); imagebits_24 = image24.scanLine(i); imagebits_8 = image8.scanLine(i); for(int j=0; jwidth; +j) int r_32 = imagebits_32 j * 4 + 2; int g_32 = imagebits_32 j * 4 + 1; int b_32 = imagebits_32 j * 4; / int r_24 = imagebits_24 j * 3; / int g_24 = imagebits_24 j *3 + 1; / int b_24 = imagebits_24 j * 3 + 2; / int gray_8 = imagebits_8 j ; /自己的操作 OK,上述两种方法的索引就不会出现图像数据偏移的问题4、大家注意到QImage的这个构造函数了吧,QImage:QImage ( uchar *data, intwidth, intheight, Formatformat) 嗯,这个函数就是从uchar* 的数据
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025湖南省社会科学院(湖南省人民政府发展研究中心)招聘高层次人才14人模拟试卷及1套参考答案详解
- 2025河北科技工程职业技术大学选聘52人考前自测高频考点模拟试题及完整答案详解1套
- 2025年商标许可使用合同5篇
- 2025广西来宾市忻城县政府投资审计中心招聘见习生2人考前自测高频考点模拟试题(含答案详解)
- 2025年上海大学公开招聘岗位(第二批)模拟试卷及参考答案详解一套
- 2025昆明市盘龙区汇承中学招聘教师(12人)模拟试卷及答案详解(易错题)
- 2025贵州天柱县第二季度(第一次)招聘8个全日制城镇公益性岗位模拟试卷含答案详解
- 2025广东省农业科学院设施农业研究所招聘劳动合同制人员1人考前自测高频考点模拟试题附答案详解(黄金题型)
- 2025届特发集团春季校园招聘模拟试卷及1套完整答案详解
- 2025年威海市水产学校公开招聘教师(7人)模拟试卷及答案详解(全优)
- 2025年初级药师资格考试试题(附答案)
- 2025广东云浮市检察机关招聘劳动合同制司法辅助人员17人备考考试题库附答案解析
- 人工智能与建筑产业体系智能化升级研究报告
- 包覆拉拔法制备铜包铝、铜包钢双金属导线的多维度探究与展望
- 大气的受热过程教学课件
- 茶叶农药知识培训课件
- 【2025秋季新修订教材】统编语文三上第六单元《19 香港璀璨的明珠》公开课一等奖创新教学设计
- 2025-2026学年人教版(2024)初中数学七年级上册教学计划及进度表
- 人教版数学二年级上册第一单元 分类与整 理 综合素养测评A卷(含答案)
- 2025版煤矿安全生产标准化管理体系考试题及答案(采煤部分)
- 油田冬季八防安全经验分享
评论
0/150
提交评论