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. tp系统常量定义

    (2013-03-06 14:16:31) 转载▼ 标签: it 是已经封装好的系统常量 主要是用在控制器下面的动作当中 这样能很大的提高我们的开发效率 主要有下面的一些      手册上面都有的   ...

  2. 安装Wamp后 Apache无法启动的解决方法

    安装Wamp后 Apache无法启动的解决方法,网上的解决方案可以说是五花八门,有些说了一大推,一点作用都起不到. 其实解决方法只需两步: 1.安装路径不能包含有中文,这个我不知道为什么,总之如果安装 ...

  3. 怎么去掉织梦网站首页带的index.html/index.php

    方法1. 1)在空间面板里面找到默认首页设置: 我们是需要去掉index.html,这时我们只需要把index.html这个把它移到最顶级去就行,然后点击确定,在打开网站刷新下,就基本可以解决了! 其 ...

  4. Linux apache的运行用户和用户组

    我们在安装apache后,有时在上传文件的时候,提示没有权限或者是不可写,我们都会去查文件夹的权限. 通过ls -l /var/www/html/website可以很直观的看出我们文件和文件夹的权限, ...

  5. ProtoBuf 与 gRPC

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

  6. Tomcat8配置Https协议,Tomcat配置Https安全访问,Tomcat Https配置

    Tomcat8配置Https协议,Tomcat配置Https安全访问,Tomcat Https配置 ============================== ©Copyright 蕃薯耀 2017 ...

  7. pthread_cond_wait的spurious wakeup问题

    最近在温习pthread的时候,忽然发现以前对pthread_cond_wait的了解太肤浅了.昨晚在看<Programming With POSIX Threads>的时候,看到了pth ...

  8. 跟我一起读postgresql源码(十四)——Executor(查询执行模块之——Join节点(下))

    3.HashJoin 节点 postgres=# explain select a.*,b.* from test_dm a join test_dm2 b on a.xxx = b.xxx; QUE ...

  9. List源码学习之LinkedList

    LinkedList 内部数据接口为一个链表,存储数据可为空可重复. 1.包含主要参数: //集合长度transient int size = 0; /** * 头结点 */ transient No ...

  10. shopnc前台登陆不进去解决方法

    安装好shopnc后,注册新用户成功,且登陆后提示登陆成功,但是一两秒后自动跳转回登陆页面,需要重新登陆问题 PHP写session不是自动起的,需要修改后才行,所以 找到PHP的php.ini配置文 ...