This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2024_Stanislav_Mykhailenko/Lesson_6/Task_1/Server/server.cpp

67 lines
1.5 KiB
C++
Raw Normal View History

2024-07-29 12:03:10 +00:00
#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) {
2024-07-29 20:48:07 +00:00
file.write((lines[line] + '\n').toUtf8());
2024-07-29 12:03:10 +00:00
}
}
file.close();
}