Qt下实现简单的UDP通信

时间:2023-03-09 09:26:38
Qt下实现简单的UDP通信

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

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

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

.pro文件

Qt下实现简单的UDP通信

这个只需要添加一行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