天哪噜,鬼知道我第一天被告知用QT做一个UDP通信的小玩意的时候,是种怎样的心情?!

哈哈,好了,言归正传。下面就是一个这样的小东西。

(原博主:http://blog.csdn.net/jdh99,因为我是初学者,也是看了原博主的博文,才知道怎么做的。分享好资源,并附上原博主未附上的ui文件)

.pro文件

这个只需要添加一行QT     += network就行。

main.cpp文件

 #include "widget.h"
#include <QApplication> int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show(); return a.exec();
}

widget.ui文件

 <?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<widget class="QTextEdit" name="txt_ip">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="txt_port_tx">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="txt_port_rx">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="txt_tx">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
</widget>
<widget class="QTextEdit" name="txt_rx">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>发送IP:</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>发送端口:</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>接收端口:</string>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>发送框</string>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>接收框</string>
</property>
</widget>
<widget class="QPushButton" name="btn_cfg">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>配置</string>
</property>
</widget>
<widget class="QPushButton" name="btn_tx">
<property name="geometry">
<rect>
<x></x>
<y></y>
<width></width>
<height></height>
</rect>
</property>
<property name="text">
<string>发送</string>
</property>
</widget>
</widget>
<layoutdefault spacing="" margin=""/>
<resources/>
<connections/>
</ui>

widget.cpp文件

 #include "widget.h"
#include "ui_widget.h" Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this); udp_socket_tx = new QUdpSocket(this);
udp_socket_rx = new QUdpSocket(this); ui->btn_tx->setEnabled(false);
} Widget::~Widget()
{
delete ui;
} void Widget::rx_udp()
{
qDebug() << "rx"; while(udp_socket_rx->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udp_socket_rx->pendingDatagramSize()); QHostAddress sender;
quint16 senderPort; udp_socket_rx->readDatagram(datagram.data(),datagram.size(),&sender,&senderPort); ui->txt_rx->append(datagram);
}
} void Widget::on_btn_tx_clicked()
{
QByteArray datagram = ui->txt_tx->toPlainText().toUtf8();
udp_socket_tx->writeDatagram(datagram, datagram.size(), Ip_Tx, Port_Tx); QString message = datagram;
ui->txt_rx->insertPlainText(message); } void Widget::on_btn_cfg_clicked()
{
bool ok;
int port_rx = ; Ip_Tx = QHostAddress(ui->txt_ip->toPlainText());
Port_Tx = ui->txt_port_tx->toPlainText().toInt(&ok); port_rx = ui->txt_port_rx->toPlainText().toInt(&ok);
udp_socket_rx->bind(QHostAddress::Any, port_rx); connect(udp_socket_rx, SIGNAL(readyRead()),this, SLOT(rx_udp())); ui->btn_tx->setEnabled(true);
}

widget.h文件

 #ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QUdpSocket> namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = );
~Widget(); private:
Ui::Widget *ui; QUdpSocket *udp_socket_tx;
QUdpSocket *udp_socket_rx;
QHostAddress Ip_Tx;
int Port_Tx; private slots:
void on_btn_cfg_clicked();
void on_btn_tx_clicked();
void rx_udp(); }; #endif // WIDGET_H

Qt下实现简单的UDP通信的更多相关文章

  1. [C++]简单的udp通信

    UDPclient.cpp #include<WINSOCK2.H> #include<iostream> #pragma comment(lib,"WS2_32.l ...

  2. QT之UDP通信

    前言:前一篇讲了TCP通信,这篇来看看UDP通信. 这里说明一下,UDP通信中分为三种通信分别为单播.组播和广播,下面将一一为大家介绍. 同样的我们都需要在工程文件中添加network QT += c ...

  3. 高性能 TCP & UDP 通信框架 HP-Socket v3.3.1

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  4. 高性能 TCP & UDP 通信框架 HP-Socket v3.2.3

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  5. 高性能 TCP & UDP 通信框架 HP-Socket v3.2.2 正式发布

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#.Del ...

  6. 高性能 TCP &amp; UDP 通信框架 HP-Socket v3.2.3 正式宣布

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件.广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...

  7. 高性能 TCP &amp; UDP 通信框架 HP-Socket v3.2.2 正式公布

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...

  8. 高性能 TCP &amp; UDP 通信框架 HP-Socket v3.2.3 正式公布

    HP-Socket 是一套通用的高性能 TCP/UDP 通信框架,包括服务端组件.client组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP 通信系统,提供 C/C++.C#. ...

  9. qt在windows下的udp通信(最简单)

    qt编程:windows下的udp通信 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 环境: 主机:win7 开发环境:qt 功能: 用udp进行收发 ...

随机推荐

  1. NetStream.appendBytes, 走向Flash P2P VOD的第一步

    之前被告知可以自行实现Flash p2p的点播功能, 但一直疑惑, 印象中NetStream并未提供相关方法, 前天看订阅时发现的文章: ByteArray Access to NetStream i ...

  2. Vmware 中安装 Ubuntu Server (或者ubuntu 以文本界面登陆时) 分辨率无法全屏问题

    Vmware 中安装 Ubuntu Server/Ubuntu 分辨率,无法全屏问题 需要更改grub设置 在终端或者文本界面按下列步骤进行设置: 第一步: 输入命令 sudo vim /etc/de ...

  3. ubuntu 14.04 中找不到 libgtk-x11-2.0.so

    如果ubuntu安装的64位的,在其中安装32位软件时就会碰到缺失libgtk-x11-2.0.so的情况 比如用wine安装qq时 启动qq时就会报这样的错误 error : cannot open ...

  4. Struts1文件上传、单文件、多文件上传【Struts1】

     将struts1文件上传的操作汇总了一下,包括单文件上传和多文件上传,内容如下,留作备忘: Struts2实现文件上传的文章(http://blog.csdn.net/itwit/article/d ...

  5. 日志组件 logback

    一.简介 Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logb ...

  6. Linux实战教学笔记12:linux三剑客之sed命令精讲

    第十二节 linux三剑客之sed命令精讲 标签(空格分隔): Linux实战教学笔记-陈思齐 ---更多资料点我查看 1,前言 我们都知道,在Linux中一切皆文件,比如配置文件,日志文件,启动文件 ...

  7. uml视频系列(二)——uml的概述

    在与uml进行了第一次的接触后,就被uml的博学多才给迷住了,uml居然可以做这么多的东西.才思敏捷的uml是设计软件的好帮手. 你还在为自己的类图不会设计而感到无助吗?你还在为你的对象不好确定而感到 ...

  8. runtime ---- iOS

    1.runtime是什么?runtime是一套底层的C语言的API(包括C语言数据类型,C语言函数) 实际上平时我们写的OC代码底层都是基于runtime,实际上也就是最后都转成了runtime代码 ...

  9. 怎么在ubuntu上运行php代码?

    1. 首先,你需要安装Apache2. sudo apt-get update sudo apt-get install apache2 当安装完以后,Apache就已经开始运行啦,你可以进行测试,通 ...

  10. FPGA学习体会

    我是安徽工程大学电子信息科学与技术专业的学生刘美花,在v3学院的培训结束了,这十几天的培训对我来说还是挺有意义的,不过中间也有一些波折.还记得刚开始的时候和老师还有各个学校的学生不太熟,心中有诸多不满 ...