您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“怎么用QT UDP网络编程实现简单消息传输”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
截图如下:
代码:
server.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QUdpSocket> #include<QtNetwork> #include<QLabel> #include<QPushButton> #include<QLineEdit> #include<QGridLayout> #include<QTimer> class Widget : public QWidget { Q_OBJECT public: 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 <QWidget> #include<QUdpSocket> #include<QtNetwork> #include<QLabel> #include<QTimer> #include<QPushButton> #include<QGridLayout> class Widget : public QWidget { Q_OBJECT public: 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 UDP网络编程实现简单消息传输”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。