QT|QT UDP网络编程实现简单消息传输
本文实例为大家分享了QT UDP实现简单消息传输的具体代码,供大家参考,具体内容如下
这几天看了下Qt的udp,顺便实现了下简单的消息传输,看起来比较简单。
UDP服务器:
截图如下:
文章图片
【QT|QT UDP网络编程实现简单消息传输】代码:
server.h
#ifndef WIDGET_H#define WIDGET_H#include#include #include #include #include #include #include #include class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0); ~Widget(); protected:QTimer* timer; QImage* image; private slots:void send(); void bordcaststart(); private:QPushButton* start_ptn; QPushButton* close_ptn; QPushButton* send_ptn; QLabel* label; QLineEdit* edit; QLabel* image_label; QGridLayout* layout; QUdpSocket* udpsocket; }; #endif // WIDGET_H
server.cpp
#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent){start_ptn=new QPushButton("start"); close_ptn=new QPushButton("quit"); send_ptn=new QPushButton("send"); label=new QLabel; label->setText("this is test!"); edit=new QLineEdit; layout=new QGridLayout(this); layout->addWidget(label); layout->addWidget(edit); layout->addWidget(send_ptn,1,1); layout->addWidget(start_ptn,2,0); layout->addWidget(close_ptn,2,1); this->resize(400,400); timer=new QTimer(this); udpsocket=new QUdpSocket(this); connect(start_ptn,SIGNAL(clicked(bool)),this,SLOT(bordcaststart())); connect(close_ptn,SIGNAL(clicked(bool)),this,SLOT(close())); //connect(timer,SIGNAL(timeout()),this,SLOT(send())); }Widget::~Widget(){}void Widget::send(){QByteArray datagram= "Broadcast message " +edit->text().toUtf8(); udpsocket->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,45454); }void Widget::bordcaststart(){// timer->start(1000); start_ptn->setDisabled(true); connect(send_ptn,SIGNAL(clicked(bool)),this,SLOT(send())); }
客户端
截图:
文章图片
client.h
#ifndef WIDGET_H#define WIDGET_H#include#include #include #include #include #include #include class Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = 0); ~Widget(); protected:private:QGridLayout* layout; QPushButton* quit_ptn; QLabel* label; QUdpSocket* udpsocket; private slots:void boarcast(); }; #endif // WIDGET_H
client.cpp
#include "widget.h"Widget::Widget(QWidget *parent): QWidget(parent){label=new QLabel; quit_ptn=new QPushButton("quit"); layout=new QGridLayout(this); layout->addWidget(label); layout->addWidget(quit_ptn); this->resize(200,200); udpsocket=new QUdpSocket(this); udpsocket->bind(45454, QUdpSocket::ShareAddress); connect(udpsocket,SIGNAL(readyRead()),this,SLOT(boarcast())); connect(quit_ptn,SIGNAL(clicked(bool)),this,SLOT(close())); }Widget::~Widget(){}void Widget::boarcast(){while (udpsocket->hasPendingDatagrams()) {QByteArray datagram; datagram.resize(udpsocket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; udpsocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort); label->setText(datagram); }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- QT实现视频传输功能
- 解决Devc++运行窗口中文乱码的实现步骤
- C++文件的数据写入和文件的数据读取的方法实现
- C语言利用UDP实现群聊聊天室的示例代码
- python|python 实现syslog 服务器的详细过程
- python证件照换底色_python利用opencv实现证件照换底
- OpenShift|OpenShift 4 - 使用 Debezium 捕获变化数据,实现MySQL到PostgreSQL数据库同步
- [博客园首发]|[博客园首发] 写连载博客,历时1410天出版书籍《物联网软件架构设计与实现》
- 技术专家说|技术专家说 | 如何基于 Spark 和 Z-Order 实现企业级离线数仓降本提效()
- 记录一个Vue3简易微信右滑删除逻辑的思路实现