Add Lesson 6 Task 1

This commit is contained in:
Stanislav Mykhailenko 2024-07-29 15:03:10 +03:00
parent 1501b2b577
commit 1af9c40b2a
GPG key ID: 1E95E66A9C9D6A36
12 changed files with 553 additions and 3 deletions

74
Lesson_6/Task_1/Client/.gitignore vendored Normal file
View file

@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View file

@ -0,0 +1,24 @@
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
client.cpp
HEADERS += \
client.h
FORMS += \
client.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

View file

@ -0,0 +1,82 @@
#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Client)
{
ui->setupUi(this);
connect (ui->b_connect, &QPushButton::clicked, this, &Client::connectPressed);
connect (ui->b_selectFile, &QPushButton::clicked, this, &Client::selectFile);
connect (ui->b_send, &QPushButton::clicked, this, &Client::sendFile);
connect (m_socket, &QTcpSocket::connected, this, &Client::connectionEstablished);
}
Client::~Client()
{
delete ui;
}
void Client::sendFile()
{
ui->l_status->setText("Connected");
QFile file(m_fileName);
QFileInfo fi(m_fileName);
if (file.open(QIODevice::ReadOnly)) {
m_socket->write((fi.fileName() + '\n').toLocal8Bit());
QTextStream in(&file);
while (!in.atEnd())
m_socket->write((in.readLine() + '\n').toLocal8Bit());
}
ui->l_status->setText("File sent");
ui->b_send->setEnabled(false);
}
void Client::connectPressed()
{
if (m_socket->isOpen()) {
m_socket->close();
ui->l_status->setText("Not connected");
ui->b_connect->setText("Connect");
ui->b_send->setEnabled(false);
return;
}
QString address = ui->e_address->text();
int port = ui->sb_port->value();
ui->b_connect->setText("Disconnect");
ui->l_status->setText("Connecting");
m_socket->connectToHost(QHostAddress(address), port);
}
void Client::connectionEstablished()
{
ui->l_status->setText("Connected");
checkSendingAllowed();
}
void Client::checkSendingAllowed()
{
if (!m_fileName.isEmpty() && m_socket->isOpen())
ui->b_send->setEnabled(true);
}
void Client::selectFile()
{
m_fileName = QFileDialog::getOpenFileName(this, "Select file", "", "All Files (*)");
if (m_fileName.isEmpty())
return;
checkSendingAllowed();
}

View file

@ -0,0 +1,37 @@
#ifndef CLIENT_H
#define CLIENT_H
#include <QMainWindow>
#include <QFileDialog>
#include <QHostAddress>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui {
class Client;
}
QT_END_NAMESPACE
class Client : public QMainWindow
{
Q_OBJECT
public:
Client(QWidget *parent = nullptr);
~Client();
private slots:
void sendFile();
void connectPressed();
void connectionEstablished();
void checkSendingAllowed();
void selectFile();
private:
Ui::Client *ui;
QTcpSocket *m_socket = new QTcpSocket();
QDataStream m_block;
QFile m_file;
QString m_fileName;
};
#endif // CLIENT_H

View file

@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Client</class>
<widget class="QMainWindow" name="Client">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>178</width>
<height>231</height>
</rect>
</property>
<property name="windowTitle">
<string>Client</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Address:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="e_address"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="sb_port">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="b_connect">
<property name="text">
<string>Connect</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_selectFile">
<property name="text">
<string>Select file</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="b_send">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Send</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>Status:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="l_status">
<property name="text">
<string>Not connected</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>178</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -0,0 +1,11 @@
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Client w;
w.show();
return a.exec();
}

74
Lesson_6/Task_1/Server/.gitignore vendored Normal file
View file

@ -0,0 +1,74 @@
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
CMakeLists.txt.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe

View file

@ -0,0 +1,19 @@
QT = core network
CONFIG += c++17 cmdline
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
server.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
server.h

View file

@ -0,0 +1,10 @@
#include <QCoreApplication>
#include <server.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Server server;
server.start("0.0.0.0", 8081);
return a.exec();
}

View file

@ -0,0 +1,66 @@
#include "server.h"
Server::Server(QObject *parent)
: QObject{parent}
{
m_server = new QTcpServer();
connect (m_server, &QTcpServer::newConnection, this, &Server::newClient);
}
Server::~Server()
{
if (m_server->isListening()) {
qDebug() << "Closing server...";
m_server->close();
}
delete m_server;
}
void Server::start(QString host, int port)
{
bool ok = m_server->listen(QHostAddress(host), port);
if (ok) {
qDebug() << "We are online!";
} else {
qDebug() << "Error creating connection: " << m_server->errorString();
}
}
void Server::newClient()
{
QTcpSocket *client = m_server->nextPendingConnection();
qDebug() << "Client " << client->localAddress().toString();
connect (client, &QTcpSocket::disconnected, this, &Server::leftClient);
connect (client, &QTcpSocket::readyRead, this, &Server::messageFromClient);
m_clients.append(client);
}
void Server::leftClient()
{
QTcpSocket *socket = (QTcpSocket *)sender();
qDebug() << "Client " << socket->localAddress().toString() << " has been left";
m_clients.removeOne(socket);
}
void Server::messageFromClient()
{
QTcpSocket *socket = (QTcpSocket *)sender();
QStringList lines = QString(socket->readAll()).split('\n');
QString fileName;
fileName = lines[0];
lines.removeFirst();
QFile file(fileName);
if (file.open(QIODevice::Append)) {
for (int line = 0; line < lines.size(); ++ line) {
file.write((lines[line] + '\n').toLocal8Bit());
}
}
file.close();
}

View file

@ -0,0 +1,38 @@
#ifndef SERVER_H
#define SERVER_H
#include <QObject>
#include <QDataStream>
#include <QFile>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QVector>
class Server : public QObject
{
Q_OBJECT
public:
explicit Server(QObject *parent = nullptr);
~Server();
public slots:
void start(QString host, int port);
private slots:
void newClient();
void leftClient();
void messageFromClient();
signals:
private:
QTcpServer *m_server = nullptr;
QVector<QTcpSocket *> m_clients;
bool isInfoGot = false;
QString fileName;
int fileSize;
signals:
};
#endif // SERVER_H

View file

@ -27,7 +27,11 @@ Checked means the task is Done.
### Lesson 5
- [x] Task 1
- [x] Task 2
- [x] Task 3
- [x] Task 4
### Lesson 6
- [ ] Task 1
- [ ] Task 2
- [ ] Task 3
- [ ] Task 4