您的位置:首页 > 编程语言 > Qt开发

Qt套接字实现UDP通信

2018-03-21 15:30 393 查看
环境:主机:win10开发环境:Qt
功能:用udp进行收发通信
界面:


源代码:1 UDPsocket.pro文件:
#-------------------------------------------------
#
# Project created by QtCreator 2018-03-21T11:13:28
#
#-------------------------------------------------
QT       += core gui
QT       += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = UDPsocket
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
mainwindow.cpp
HEADERS  += mainwindow.h
FORMS    += mainwindow.ui
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QUdpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *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 // MAINWINDOW_H
2 MainWindow.h文件:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork/QUdpSocket>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *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 // MAINWINDOW_H
3 MainWindow.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udp_socket_tx = new QUdpSocket(this);
udp_socket_rx = new QUdpSocket(this);
ui->btn_tx->setEnabled(false);
ui->txt_ip->setText("172.16.7.61");
}
MainWindow::~MainWindow()
{
delete ui;
}
//接收udp数据
void MainWindow::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 MainWindow::on_btn_tx_clicked()
{
QByteArray datagram = ui->txt_tx->toPlainText().toAscii();
udp_socket_tx->writeDatagram(datagram, datagram.size(), Ip_Tx, Port_Tx);
}
//配置按键
void MainWindow::on_btn_cfg_clicked()
{
bool ok;
int port_rx = 0;
//获得发送IP和端口
Ip_Tx = QHostAddress(ui->txt_ip->text());
Port_Tx = ui->txt_port_tx->text().toInt(&ok);
//获得接收端口
port_rx = ui->txt_port_rx->text().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);
}
4 Main.cpp文件:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}


效果:



到这里,实现了UDP双向通信功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐