上周末没事写了一个UDP的例子 大家感兴趣的可以参考一下
来源:互联网 发布:sql语句优化方法 编辑:程序博客网 时间:2024/05/16 10:23
一直以来大家都是用TCP/IP通信的 或者是用端口进行通信的,上周末没事写了一个UDP 相当于局域网广播通信。
废话少说直接贴代码 ,可以传输英文 中文今天调通了
发送之前做中文处理,接收之后也需要做一下正文处理(utf8)
环境:qt5.3.2 win8 64位
一、UDPclient(头文件和源文件)
头文件:
#ifndef MAINDIALOG_H
#define MAINDIALOG_H
#include <QDialog>
#include <QtNetwork/QUdpSocket>
#include <QTextCodec>
namespace Ui {
class MainDialog;
}
class MainDialog : public QDialog
{
Q_OBJECT
public:
explicit MainDialog(QWidget *parent = 0);
~MainDialog();
private slots:
void on_startPushButton_clicked();
private:
Ui::MainDialog *ui;
QUdpSocket* udpSocket;
};
#endif // MAINDIALOG_H
源文件:
#include "maindialog.h"
#include "ui_maindialog.h"
#include <QString>
#include <QTextCodec>
#include <QDebug>
MainDialog::MainDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MainDialog)
{
ui->setupUi(this);
this->setWindowTitle(tr("局域网广播"));
udpSocket = new QUdpSocket(this);
}
MainDialog::~MainDialog()
{
delete ui;
}
void MainDialog::on_startPushButton_clicked()
{
QString sendDats = ui->senderDatasTextEdit->toPlainText();
QString port = ui->connectPortLineEdit->text();
QByteArray sendByte = sendDats.toUtf8();
qDebug()<<sendDats;
udpSocket->writeDatagram(sendByte,QHostAddress::LocalHost,port.toInt());
}
二、UDPService
头文件:
#ifndef SERIVEDIALOG_H
#define SERIVEDIALOG_H
#include <QDialog>
#include <QtNetwork/QUdpSocket>
#include <QDebug>
#include <QTextCodec>
namespace Ui {
class SeriveDialog;
}
class SeriveDialog : public QDialog
{
Q_OBJECT
public:
explicit SeriveDialog(QWidget *parent = 0);
~SeriveDialog();
public slots:
void dealWithTheDate();
private slots:
void on_startRecvPushButton_clicked();
private:
Ui::SeriveDialog *ui;
QUdpSocket* udpsocket;
QString port;
};
#endif // SERIVEDIALOG_H
源文件:
#include "serivedialog.h"
#include "ui_serivedialog.h"
SeriveDialog::SeriveDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SeriveDialog)
{
ui->setupUi(this);
udpsocket = new QUdpSocket(this);
this->setWindowTitle(tr("局域网广播"));
}
SeriveDialog::~SeriveDialog()
{
delete ui;
}
void SeriveDialog::dealWithTheDate()
{
QByteArray recvDatas;
if(udpsocket->hasPendingDatagrams())
{
recvDatas.resize(udpsocket->pendingDatagramSize());
udpsocket->readDatagram(recvDatas.data(),recvDatas.size());
}
qDebug()<<recvDatas;
// ui->textBrowser->append(recvDatas);
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
//QTextCodec* codec = QTextCodec::codecForName("GB18030");
QString all = codec->toUnicode(recvDatas);
qDebug()<<all;
// 显示在text上
ui->textBrowser->append(all);
}
void SeriveDialog::on_startRecvPushButton_clicked()
{
port = ui->protLineEdit->text();
udpsocket->bind(QHostAddress::LocalHost,port.toInt());
connect(udpsocket,SIGNAL(readyRead()),this,SLOT(dealWithTheDate()));
}
三、效果:
先运行服务器
- 上周末没事写了一个UDP的例子 大家感兴趣的可以参考一下
- 写了一个合并的,大家可以参考下
- 给大家做了一个关于日程的例子,可以参考一下,对进一步学习有所帮助
- 一个关于Applet的例子,需要的可以参考一下!
- 前一段时间没事用c#写了一个串口程序,这里总结一下自己的经验
- 上周末去安吉照了两张照片,贴出来大家一起欣赏一下!(原创)
- 关于jsp上传文件的代码,大家可以参考一下。
- 最近闲暇无事,做了一个UDP监控的程序,贴出源码,供大家参考。
- 06 最近做的一个播客站点, 请大家没事的时候'玩儿'一下
- Java、Android中的回调(callBack)我写的一个网络回调大家可以参考参考
- 没事的时候可以看一下,学习一下
- 刚做的一个网页计数器 大家参考一下
- 刚做的一个网页计数器 大家参考一下
- 进程与线程一个很好的类比,大家参考一下
- 别人写的通信录,以后可以参考一下
- 参考网上的一些struts2的action用法,自己写了一个Demo记录一下。
- 今天写了一个webservice的loadrunner脚步和大家分享一下
- 写了一个ES6(ES2015)模块可用的xml2json插件,给大家分享一下
- 快速排序(Quick Sort)
- geoserver默认密码修改
- 转载softmax 归一函数
- Android中全屏设置
- Android Gradle plugin 0.7.0: “duplicate files during packaging of APK”
- 上周末没事写了一个UDP的例子 大家感兴趣的可以参考一下
- Android官方开发文档Training系列课程中文版:管理音频播放之管理音频焦点
- 进制转换
- Storm中Spout和Bolt的生命周期
- FZU 2093 寻找兔子 (状态压缩dp)
- 友盟社会化分享
- 在无头单链表的一个非头节点前插入一个节点(C语言)
- hdoj 3371 Connect the Cities
- Late binding