Qt---基于UDP的网络广播程序
2016-07-06 23:30
816 查看
服务器端
初始化中创建QUdpSocket、QTimer,当计时器超时,向客户端发送文本框中的数据。计时器每隔1s重启一次。
.pro
QT += network
udpserver.h
#ifndef UDPSERVER_H #define UDPSERVER_H #include <QDialog> #include <QLabel> #include <QLineEdit> #include <QPushButton> #include <QVBoxLayout> #include <QUdpSocket> #include <QTimer> class UdpServer: public QDialog { Q_OBJECT public: UdpServer(QWidget *parent=0, Qt::WindowFlags f = 0); ~UdpServer(); public slots: void StartBtnClicked(); void timeout(); private: QLabel *TimerLabel; QLineEdit *TextLineEdit; QPushButton *StartBtn; QVBoxLayout *mainLayout; int port; bool isStarted; QUdpSocket *udpSocket; QTimer *timer; }; #endif // UDPSERVER_H
udpserver.cpp
#include "udpserver.h" #include <QHostAddress> UdpServer::UdpServer(QWidget *parent, Qt::WindowFlags f) { setWindowTitle(tr("UDP Server")); TimerLabel = new QLabel(tr("计时器:")); TextLineEdit = new QLineEdit(this); StartBtn = new QPushButton(tr("开始"), this); mainLayout = new QVBoxLayout(this); mainLayout->addWidget(TimerLabel); mainLayout->addWidget(TextLineEdit); mainLayout->addWidget(StartBtn); connect(StartBtn, SIGNAL(clicked()), this, SLOT(StartBtnClicked())); isStarted = false; port = 5555; udpSocket = new QUdpSocket(this); timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(timeout())); } UdpServer::~UdpServer() { } void UdpServer::StartBtnClicked() { if(!isStarted) { StartBtn->setText(tr("停止")); timer->start(1000); //每隔1秒启动一次 isStarted = true; } else { StartBtn->setText(tr("开始")); isStarted = false; timer->stop(); } } //向端口发送广播信息 void UdpServer::timeout() { QString msg = TextLineEdit->text(); int length = 0; if(msg == "") return; if((length == udpSocket->writeDatagram(msg.toLatin1(), msg.length(), QHostAddress::Broadcast, port)) != msg.length()) return; }
main.cpp
#include "udpserver.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); UdpServer w; w.show(); return a.exec(); }
客户端
初始化后中创建QUdpSocket,绑定端口,当接收到数据时,显示到文本框中
.pro
QT += network
udpclient.h
#ifndef UDPCLIENT_H #define UDPCLIENT_H #include <QDialog> #include <QVBoxLayout> #include <QTextEdit> #include <QPushButton> #include <QUdpSocket> class UdpClient:public QDialog { Q_OBJECT public: UdpClient(QWidget *parent=0, Qt::WindowFlags f = 0); ~UdpClient(); public slots: void CloseBtnClicked(); void dataReceived(); private: QTextEdit *ReceiveTextEdit; QPushButton *CloseBtn; QVBoxLayout *mainLayout; int port; QUdpSocket *udpSocket; }; #endif // UDPCLIENT_H
udpserver.cpp
#include "udpclient.h" #include <QMessageBox> #include <QHostAddress> UdpClient::UdpClient(QWidget *parent, Qt::WindowFlags f):QDialog(parent) { setWindowTitle(tr("UDP Client")); ReceiveTextEdit = new QTextEdit(this); CloseBtn = new QPushButton(tr("Close"), this); mainLayout = new QVBoxLayout(this); mainLayout->addWidget(ReceiveTextEdit); mainLayout->addWidget(CloseBtn); connect(CloseBtn, SIGNAL(clicked()), this, SLOT(CloseBtnClicked())); port = 5555; udpSocket = new QUdpSocket(this); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(dataReceived())); bool result = udpSocket->bind(port); if(!result) { QMessageBox::information(this, "error", "udp socket create error!"); return; } } UdpClient::~UdpClient() { } void UdpClient::CloseBtnClicked() { close(); } void UdpClient::dataReceived() { while(udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); udpSocket->readDatagram(datagram.data(), datagram.size()); ReceiveTextEdit->insertPlainText(datagram.data()); } }
main.cpp
#include "udpclient.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); UdpClient w; w.show(); return a.exec(); }
相关文章推荐
- udp与tcp通信代码
- HTTP协议
- HTTP Servlet 重要的几个方法
- 学习TensorFlow,TensorBoard可视化网络结构和参数
- 学习TensorFlow,TensorBoard可视化网络结构和参数
- UNIX网络编程——shutdown函数(I/O复用并发服务器)
- 使用 acl 协程编写高并发网络服务
- WiFi时,显示图片高清图;网络状态为蜂窝移动网络时,显示图片缩略图http://gold.xitu.io/entry/57285c5e2e958a0068db2f9e
- AJAX入门 之 XMLHttpRequest 状态与 onreadystatechange 事件(三)
- 使用WebService进行网络编程【工具类】
- Linux下的C语言开发(网络编程)
- Qt---获取本机网络信息
- 学习笔记——网络基础(一)
- wifi eth gprs 网络自动切换 脚本
- 通过HttpServletRequest获取上一个页面
- HTTP中GET和POST的区别
- tensorflow入门简单卷积神经网络
- 高性能HTTP加速器Varnish安装与配置
- HTTP协议
- httpClient