功能:
用udp进行收发通信
界面:
源代码:
LssHost.pro:
[cpp]
#-------------------------------------------------
#
# Project created by QtCreator 2013-09-22T09:36:44
#
#-------------------------------------------------
QT += core gui
QT += network
TARGET = LssHost
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindows.h
[cpp]
#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
mainwindows.cpp:
[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);
}
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(),