Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dépôt versions Qt sockets UDP en réception
À la suite de la publication du support sur l'utilisation des sockets en langage C, voici deux déclinaisons du programme serveur UDP. Comme ces programmes s'adressent à un public de débutant, on propose une première version qui utilise juste les appels de bibliothèques Qt sans aucune «interaction» graphique puis une seconde version avec une fenêtre dans laquelle les messages ainsi que les adresses IP et numéros de port des émetteurs s'affichent.
- Loading branch information
Showing
7 changed files
with
295 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2012-02-26T20:55:23 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
QT += network | ||
|
||
TARGET = gui-udp-receiver | ||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp\ | ||
mainwindow.cpp | ||
|
||
HEADERS += mainwindow.h | ||
|
||
FORMS += mainwindow.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <QtGui/QApplication> | ||
#include "mainwindow.h" | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
MainWindow w; | ||
|
||
w.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include "mainwindow.h" | ||
#include "ui_mainwindow.h" | ||
#include <QtCore/QTextStream> | ||
#include <QtCore/QDebug> | ||
#include <QtNetwork> | ||
|
||
MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | ||
ui(new Ui::MainWindow) | ||
{ | ||
|
||
listenSocket = new QUdpSocket(this); | ||
|
||
if (listenSocket->bind(QHostAddress::Any, 8888) < 0) { | ||
qDebug("Impossible de créer le socket en écoute"); | ||
exit(1); | ||
} | ||
|
||
ui->setupUi(this); | ||
|
||
QObject::connect(listenSocket, SIGNAL(readyRead()), | ||
this, SLOT(processPendingDatagrams())); | ||
|
||
QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(close())); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void MainWindow::processPendingDatagrams() | ||
{ | ||
|
||
while (listenSocket->hasPendingDatagrams()) { | ||
QByteArray datagram; | ||
datagram.resize(listenSocket->pendingDatagramSize()); | ||
listenSocket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); | ||
|
||
msg = datagram.data(); | ||
ui->senderLabel->setText(tr("Depuis : %1:%2") .arg(senderAddress.toString()) .arg(senderPort)); | ||
ui->msgLabel->setText(tr("Message : \"%1\"").arg(msg)); | ||
|
||
msg = msg.toUpper(); | ||
qDebug() << datagram.data(); | ||
|
||
datagram.clear(); | ||
datagram.append(msg); | ||
|
||
if (listenSocket->writeDatagram(datagram, senderAddress, senderPort) < 0) { | ||
qDebug("Émission du message modifié impossible"); | ||
listenSocket->close(); | ||
exit(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <QMainWindow> | ||
#include <QtGui> | ||
#include <QtCore/QTextStream> | ||
#include <QtCore/QDebug> | ||
#include <QtNetwork> | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = 0); | ||
~MainWindow(); | ||
|
||
private slots: | ||
void processPendingDatagrams(); | ||
|
||
private: | ||
Ui::MainWindow *ui; | ||
QLabel *senderLabel, *msgLabel; | ||
QUdpSocket *listenSocket; | ||
QHostAddress senderAddress; | ||
quint16 senderPort; | ||
QString msg; | ||
|
||
}; | ||
|
||
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>1054</width> | ||
<height>231</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QWidget" name="centralWidget"> | ||
<widget class="QFrame" name="frame"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>30</x> | ||
<y>10</y> | ||
<width>991</width> | ||
<height>91</height> | ||
</rect> | ||
</property> | ||
<property name="frameShape"> | ||
<enum>QFrame::StyledPanel</enum> | ||
</property> | ||
<property name="frameShadow"> | ||
<enum>QFrame::Raised</enum> | ||
</property> | ||
<widget class="QLabel" name="senderLabel"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>20</x> | ||
<y>10</y> | ||
<width>471</width> | ||
<height>31</height> | ||
</rect> | ||
</property> | ||
<property name="text"> | ||
<string>Emetteur</string> | ||
</property> | ||
</widget> | ||
<widget class="QLabel" name="msgLabel"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>20</x> | ||
<y>50</y> | ||
<width>921</width> | ||
<height>19</height> | ||
</rect> | ||
</property> | ||
<property name="text"> | ||
<string>Attente d'un nouveau message</string> | ||
</property> | ||
</widget> | ||
</widget> | ||
<widget class="QPushButton" name="pushButton"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>40</x> | ||
<y>120</y> | ||
<width>118</width> | ||
<height>28</height> | ||
</rect> | ||
</property> | ||
<property name="text"> | ||
<string>Quitter</string> | ||
</property> | ||
</widget> | ||
</widget> | ||
<widget class="QMenuBar" name="menuBar"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>1054</width> | ||
<height>25</height> | ||
</rect> | ||
</property> | ||
</widget> | ||
<widget class="QToolBar" name="mainToolBar"> | ||
<attribute name="toolBarArea"> | ||
<enum>TopToolBarArea</enum> | ||
</attribute> | ||
<attribute name="toolBarBreak"> | ||
<bool>false</bool> | ||
</attribute> | ||
</widget> | ||
<widget class="QStatusBar" name="statusBar"/> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
#include <QtCore/QCoreApplication> | ||
#include <QtCore/QTextStream> | ||
#include <QtCore/QDebug> | ||
#include <QtNetwork> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QCoreApplication a(argc, argv); | ||
|
||
bool go_on_like_this; | ||
QUdpSocket listenSocket; | ||
QByteArray datagram; | ||
QHostAddress senderAddress; | ||
quint16 senderPort; | ||
QString msg; | ||
|
||
if (listenSocket.bind(QHostAddress::Any, 8888) < 0) { | ||
qDebug("Impossible de créer le socket en écoute"); | ||
go_on_like_this = false; | ||
} | ||
else | ||
go_on_like_this = true; | ||
|
||
while (go_on_like_this) { | ||
|
||
if (listenSocket.waitForReadyRead(100)) { | ||
if (listenSocket.hasPendingDatagrams()) { | ||
datagram.resize(listenSocket.pendingDatagramSize()); | ||
listenSocket.readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); | ||
msg = datagram.data(); | ||
|
||
qDebug() << "Depuis : " << senderAddress.toString() << ':' << senderPort; | ||
qDebug() << "Message : " << msg; | ||
|
||
msg = msg.toUpper(); | ||
datagram.clear(); | ||
datagram.append(msg); | ||
|
||
if (listenSocket.writeDatagram(datagram, senderAddress, senderPort) < 0) { | ||
qDebug("Émission du message modifié impossible"); | ||
listenSocket.close(); | ||
exit(1); | ||
} | ||
} | ||
|
||
if (msg == "_EXIT_") { | ||
listenSocket.close(); | ||
go_on_like_this = false; | ||
exit(0); | ||
} | ||
} | ||
} | ||
|
||
listenSocket.close(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2012-02-26T17:43:37 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core | ||
QT += network | ||
|
||
QT -= gui | ||
|
||
TARGET = nogui-udp-receiver | ||
CONFIG += console | ||
CONFIG -= app_bundle | ||
|
||
TEMPLATE = app | ||
|
||
|
||
SOURCES += main.cpp |