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. Java中Calendar.DAY_OF_WEEK、DAY_OF_MONTH需要减一的原因

    Java中对日期的处理需要用到Calendar类,其中有几个方法在使用时需要新手注意.1. 在获取月份时,Calendar.MONTH + 1 的原因(Java中Calendar.MONTH返回的数值 ...

  2. leak finder

    介绍 leak finder 是google开源团队发布了一个新的可以帮助web应用程序开发者在他们的JavaScript程序中找出内存泄露问题的工具: http://feedproxy.google ...

  3. Spark应用_PageView_UserView_HotChannel

    Spark应用_PageView_UserView_HotChannel 一.PV 对某一个页面的访问量,在页面中进行刷新一次就是一次pv PV {p1, (u1,u2,u3,u1,u2,u4-)} ...

  4. c++---天梯赛---大笨钟

    ★题目: ★思路分析: 对可能的情况进行分类处理.在这里我把它们分成了3大类. ①不在敲钟时间 ②在敲钟时间但为整点 ③在敲钟时间且不为整点. 在敲钟时间段内我们可分别对晚8点前后进行分类讨论, 我们 ...

  5. JAVA 键盘输入数组,输出数组内容和最大值、最小值

    package shuzu; import java.util.Scanner; import java.util.Arrays; public class shuzu { /** * @param ...

  6. Solr学习笔记2(V7.2)---导入自己的数据

    学而不思则罔,思而不学则殆,总是看文档不动手效果是不好的.没有实地的从自己的数据库获取数据测试一下始终是空,总结一下自己的操作步骤吧. 第一步准备配置文件 E:\Solr\server\solr\co ...

  7. asp.net -mvc框架复习(7)-基于MVC搭建用户登录项目框架

    整体框架: 一.搭建Model层 1.添加通用数据访问类 2.添加实体类(封装和传递数据,和数据库中数据表对应) 3.添加数据访问类(通常和实体类同名,但是后缀名发生改变) 二.搭建控制器层Contr ...

  8. Weblogic jsp页面编译出错,Weblogic jsp编译异常

    Weblogic jsp页面编译出错,Weblogic jsp编译异常 ======================== 蕃薯耀 2018年1月29日 http://www.cnblogs.com/f ...

  9. spring-mvc整合jquery cropper图片裁剪插件

    参考网址:http://blog.csdn.net/u012759397/article/details/53126522

  10. JavaScript ES6 let、const

    在ES6中,增加了2个声明变量的关键字:let 和 const.在这里将详细介绍let与var的区别.Babel对let的处理以及const的简单使用. 1. let 在ES6规范中增加了 let 关 ...