UDP
QUdpSocket ---> upd socket

1.创建
QUdpSocket *p = new QUdpSocket();

2.最先接收数据一方 调用bind-> ip/port
bool QAbstractSocket::bind(const QHostAddress & address, quint16 port = 0, BindMode mode = DefaultForPlatform)
p->bind();

connect( ,SIGNAL(readyRead()),,SLOT(r));
3.接收数据
当 QUdpSocket 对象收到别一方发来的数据会发出信号 readyRead()
qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
r()
{
p->readDatagram();

}
4.发数据
qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)
p->writeDatagram();

recv.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QHostAddress>
#include<QUdpSocket>
#include<QByteArray>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void getmsg();
void on_pushButton_2_clicked(); private:
Ui::Dialog *ui;
QUdpSocket *up;
QHostAddress *host;
quint16 port,port1;
}; #endif // DIALOG_H
recv.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this); up = new QUdpSocket(this);
port = ; } Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
up->bind(QHostAddress("192.168.1.30"),);
connect(up,SIGNAL(readyRead()),this,SLOT(getmsg()));
}
void Dialog::getmsg()
{//qDebug()<<host<<port;
QByteArray buf;
buf.resize(up->pendingDatagramSize());
QHostAddress host1;
// quint16 port1;
//qDebug()<<host<<port;
up->readDatagram(buf.data(),buf.size(),&host1,&port1);
// qDebug()<<buf.data();
QString tmp(buf.data());
ui->listWidget->addItem(tmp);
} void Dialog::on_pushButton_2_clicked()
{//qDebug()<<host<<port;
host = new QHostAddress("192.168.1.30"); QString tmp = ui->lineEdit->text();
qDebug()<<tmp; up->writeDatagram(tmp.toLatin1(),tmp.size(),*host,port1);
//qDebug()<<host<<port; }
ui
/********************************************************************************
** Form generated from reading UI file 'dialog.ui'
**
** Created by: Qt User Interface Compiler version 5.5.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/ #ifndef UI_DIALOG_H
#define UI_DIALOG_H #include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QPushButton> QT_BEGIN_NAMESPACE class Ui_Dialog
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QListWidget *listWidget;
QLabel *label;
QLineEdit *lineEdit; void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(, );
pushButton = new QPushButton(Dialog);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(, , , ));
pushButton_2 = new QPushButton(Dialog);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
pushButton_2->setGeometry(QRect(, , , ));
listWidget = new QListWidget(Dialog);
listWidget->setObjectName(QStringLiteral("listWidget"));
listWidget->setGeometry(QRect(, , , ));
label = new QLabel(Dialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(, , , ));
lineEdit = new QLineEdit(Dialog);
lineEdit->setObjectName(QStringLiteral("lineEdit"));
lineEdit->setGeometry(QRect(, , , )); retranslateUi(Dialog); QMetaObject::connectSlotsByName(Dialog);
} // setupUi void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", ));
pushButton->setText(QApplication::translate("Dialog", "bind", ));
pushButton_2->setText(QApplication::translate("Dialog", "send", ));
label->setText(QApplication::translate("Dialog", "recv", ));
} // retranslateUi }; namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui QT_END_NAMESPACE #endif // UI_DIALOG_H
send.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QDebug>
#include<QUdpSocket>
#include<QHostAddress>
#include<QByteArray>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void getmsg();
private:
Ui::Dialog *ui;
QUdpSocket *up;
QHostAddress *host,host1;
quint16 port,port1;
}; #endif // DIALOG_H
send.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// up = new QUdpSocket;
port = ;
host = new QHostAddress("192.168.1.30");
up = new QUdpSocket();
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{ QString tmp = ui->lineEdit->text();
qDebug()<<tmp;
up->writeDatagram(tmp.toLatin1(),*host,port);
connect(up,SIGNAL(readyRead()),this,SLOT(getmsg()));
}
void Dialog::getmsg()
{
QByteArray buf;
buf.resize(up->pendingDatagramSize());
QHostAddress host1;
quint16 port1;
up->readDatagram(buf.data(),buf.size(),&host1,&port1);
//qDebug()<<buf.data();
QString tmp(buf.data());
ui->listWidget->addItem(tmp);
}

qt中的udp编程的更多相关文章

  1. QT中的SOCKET编程(QT-2.3.2)

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...

  2. QT中的SOCKET编程

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...

  3. Qt中的多线程编程

    http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...

  4. Qt中的串口编程之三

    QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...

  5. 5.关于QT中的网络编程,QTcpSocket,QUdpSocket

     1 新建一个项目:TCPServer.pro A  修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...

  6. Qt下的udp编程

    项目需要一个基于udp的客户端, 看着Qt是有个QUdpSocket的类的, 但自带的例子和类的说明都没咋说明白: 怎么用一个QUdpSocket既当发送端, 又当接收端? 谷歌一顿后, 发现很多老内 ...

  7. Qt中的串口编程之一

    QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...

  8. golang中的udp编程

    1. udp server package main import ( "fmt" "net" ) func main() { // udp server li ...

  9. qt中的tcp编程

    server .server.h #define DIALOG_H #include <QDialog> #include <QTcpServer> #include < ...

随机推荐

  1. 【转自知乎】:localhost、127.0.0.1 和 本机IP 三者的区别?

    作者:知乎用户链接:https://www.zhihu.com/question/23940717/answer/26230963来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  2. Maven中央仓库源地址改为阿里云(IDEA)

    我的Maven是IDEA2017.1.2集成的,所以settings.xml在此位置 E:\Program Files\JetBrains\IntelliJ IDEA 2017.1.2\plugins ...

  3. windows 防火墙拦截nginx的问题

    今天在azure vm上安装了nginx并配置了代理设置,但域名访问始终无法中转,一开始怀疑是nginx的服务没起来,但在本地访问localhost看下如下界面,证明服务是没问题的. 本地访问没问题, ...

  4. ProtoBuf 与 gRPC

    用 Protobuf 很久了,但是一直觉得很简单,所以就没有做一个总结,今天想尝试一下 gRPC,顺带就一起总结一下.ProtoBuf 是个老同志了,应该是 2010 的时候发布的,然后被广泛使用,目 ...

  5. [SinGuLaRiTy] 复习模板-高精度模板

    [SinGuLaRiTy-1042] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 结构体封装 //高精度运算 注意%I64d与%lld # ...

  6. python实现端口扫描器/DoS/DDoS

    整理github,梳理下Python小工具.以下是python实现的DoS/DDoS/端口扫描器(github). 一.DoS SYN Flood是当前最流行的DoS(拒绝服务攻击)与DdoS(分布式 ...

  7. Node.js之单利模式

    在iOS中我们经常用到单利模式,这样就能够实现在多处共享同一数据,那么在Node.js中也存在这种模式. 我们知道,Node.js中模块的概念很重要,在写模块的接口的时候,只需要暴露出一个实例对象就能 ...

  8. Linux指令--tar,gzip

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...

  9. 易趣:使用MongoDB创建关键业务的多数据中心应用

    eBay:使用MongoDB创建关键业务的多数据中心应用 作为全球前十的零售品牌,eBay的活跃用户有一亿七千多万,并拥有跨越全世界190个市场的10亿购物清单,这样的规模下,eBay绝对不允许出现宕 ...

  10. Struts2 (一)

    1 三层架构 2 MVC框架的原理 3 什么是Struts2 Struts2是一个非常优秀的MVC框架,基于Model2设计模式. 是由传统的Struts1和WebWork两个经典的框架发展而来的. ...