QUdpSocket使用整理
来源:互联网 发布:经济大数据分析书籍 编辑:程序博客网 时间:2024/05/16 03:07
效果图
服务端源码
注意:在.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_OBJECTpublic: UdpServer(QWidget *parent = 0,Qt::WindowFlags f=0); ~UdpServer();public slots: void StartBtnClicked();//点击开始按钮 void timeout();//一段时间后执行函数private: QLabel *TimerLabel; QLineEdit *TextLineEdit;//发送内容的lineedit QPushButton *StartBtn;//开始按钮 QVBoxLayout *mainLayout;//布局 int port;//端口号 bool isStarted;//是否开始 QUdpSocket *udpSocket;//udp QTimer *timer;//计时器};#endif // UDPSERVER_H
udpserver.cpp
#include "udpserver.h"#include <QHostAddress>UdpServer::UdpServer(QWidget *parent, Qt::WindowFlags f) : QDialog(parent,f){ setWindowTitle(tr("UDP Server")); TimerLabel = new QLabel(tr("计时器:"),this); 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())); port =5555; isStarted=false; 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秒执行一次timeout() isStarted =true; } else { StartBtn->setText(tr("开始")); isStarted = false; timer->stop();//停止 }}void UdpServer::timeout(){ QString msg = TextLineEdit->text(); int length=0; if(msg=="") { return; } //通过udp写入传输数据 if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length()) { return; }}
客户端源码
注意:在.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_OBJECTpublic: 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;//udp};#endif // UDPCLIENT_H
udpclient.cpp
#include "udpclient.h"#include <QMessageBox>#include <QHostAddress>UdpClient::UdpClient(QWidget *parent,Qt::WindowFlags f) : QDialog(parent,f){ 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,tr("error"),tr("udp socket create error!")); return; }}UdpClient::~UdpClient(){}void UdpClient::CloseBtnClicked(){ close();}void UdpClient::dataReceived(){ //通过udp接收数据 while(udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); //读取数据 udpSocket->readDatagram(datagram.data(),datagram.size()); QString msg=datagram.data(); //向textdit中插入数据 ReceiveTextEdit->insertPlainText(msg); }}
阅读全文
0 0
- QUdpSocket使用整理
- 使用QUdpSocket进行UDP编程
- QUdpSocket和QTcpSocket的使用
- 使用QUdpSocket时编译出错的问题
- qt学习之QUdpSocket的使用
- QT 使用QUdpSocket QUdpServer UDP 建立客户端与服务器端
- QUdpSocket Class
- 初识QUdpsocket
- 中标麒麟(linux)下QUdpSocket无法使用组播发送数据问题
- QUdpSocket简单的使用(局域网内发送给所有客户端消息与接收)
- QUdpSocket相关 (1)
- qt4 udp QUdpSocket -------wireshark
- QUdpSocket的问题
- QUdpSocket 简单用法
- Qt(3)QUdpsocket
- QUdpSocket进行网络传输
- QT QUdpSocket广播
- QUdpSocket实现简单通信
- git命令
- 网络与分布式计算复习 名词解释与简答
- tensorflow LSTM + CTC实现端到端OCR
- 对内存对齐和位域的理解
- 帧布局典型例子实战
- QUdpSocket使用整理
- hp主机系统的开关机和PDC简介
- Linux下的环境变量
- 判断点是否在三角形内
- java语言基础----(特性方面)
- 笔记本键盘上输入0变成/怎么回事?
- html的onclick跳转
- Bear and Three Balls CodeForces
- windows下键盘的输入