天哪噜,鬼知道我第一天被告知用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. MyBatis绑定错误--BindingException:Invalid bound statement (not found)

    如果出现: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 一般的原因是Mapper i ...

  2. ios NSString拼接方法总结

    NSString* string; // 结果字符串 02 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 03   04 / ...

  3. easyui帮助文档地址

    http://www.jeasyui.com/documentation/index.php# http://www.jeasyui.net/tutorial/22.html http://www.j ...

  4. 只为粗暴看一下ES6的字符串模板的性能

    网上查找"ES6 字符串模板 +性能"5分钟无果遂写了一个暴力测试. 测试对象: +=方式,字符串累加计算方式 +s1+s2...+sn方式,即传统连加拼接字符串方式 s.push ...

  5. [C#]为微软ASP.NET官方教学视频增加字幕

    前言 Microsoft Virtual Academy提供了学习ASP.NET的大量视频材料.(注1) 由于视频服务器位于海外,国内浏览速度并不理想,幸好官方提供了视频的下载地址以及英文字幕文件. ...

  6. [JQuery] Ajax使用过程中的问题总结

    JQuery提供的ajax函数,在使用过程中,因为对参数的不了解,导致了很多错误,现在总结如下,以便时常温固,不犯同样的错误. 1.我在项目中使用到的ajax请求格式如下: $.ajax({ url: ...

  7. [TPYBoard-Micropython之会python就能做硬件 4] 学习使用电位器和1602显示屏

    一.实验器材 1.TPYboard V102板  一块 2.电位器   一个 3.1602 屏 一块 4.杜邦线:若干 二.电位器的使用 电位器 (英文:Potentiometer)是可变电阻器的一种 ...

  8. 虚拟机网络配置详解(NAT、桥接、Hostonly)

    VirtualBox中有四种网络连接方式: NAT Bridged Adapter Internal Host-only Adapter VMWare中有三种,其实它跟VMWare的网络连接方式都是一 ...

  9. PHP标准库(SPL)- SplDoublyLinkedList类(双向链表)

    class SplDoublyLinkedList implements Iterator, Traversable, Countable, ArrayAccess { const IT_MODE_L ...

  10. UUID错误

    在Archive项目时,出现了“Your build settings specify a provisioning profile with the UUID “”, however, no suc ...