-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyTcpServer.cpp
50 lines (44 loc) · 1.28 KB
/
MyTcpServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**************************************************
* Copyright (C) 2021 ARAD. All Rights Reserved. *
***************************************************/
#include <QFile>
#include "MyTcpServer.h"
MyTcpServer::MyTcpServer(int port, QObject *parent):
QObject(parent), _port(port)
{
server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection,
this, &MyTcpServer::newConnection);
if (!server->listen(QHostAddress::LocalHost, port))
{
qDebug() << "Server could not start";
}
else
{
qDebug() << "Server started!";
}
}
void MyTcpServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
QString rfname;
socket->waitForReadyRead(3000);
rfname = socket->readAll();
qDebug() << rfname;
socket->waitForReadyRead(3000);
QFile fileFromClient("./" + rfname);
QByteArray bytesReceived = socket->readAll();
socket->flush();
// fileFromClient.setFileName("/home/nrezayi/Desktop/downloaded/f.txt");
if (fileFromClient.open(QIODevice::ReadWrite))
{
fileFromClient.write(bytesReceived, bytesReceived.length());
fileFromClient.close();
qDebug() << "file " << rfname << " has been received";
}
else
{
qDebug() << "file has NOT been received";
}
qDebug() << "end of file transfer...";
}