-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockettest.cpp
113 lines (91 loc) · 2.71 KB
/
sockettest.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**************************************************
* Copyright (C) 2021 ARAD. All Rights Reserved. *
***************************************************/
#include "sockettest.h"
#include <QTcpSocket>
#include <QFile>
#include <QDir>
#include <QByteArray>
#include <QHostAddress>
#include <string>
SocketTest::SocketTest(const QString &fpathStr, const qint16 &portValInt, QObject *parent):
QObject(parent), _fpathStr(fpathStr), _portVal(portValInt)
{
}
void SocketTest::sendfile(QString path)
{
// code of socket construction
socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::connected, this, &SocketTest::connected);
connect(socket, &QTcpSocket::disconnected, this, &SocketTest::disconnected);
connect(socket, &QTcpSocket::bytesWritten, this, &SocketTest::bytesWritten);
connect(socket, &QTcpSocket::readyRead, this, &SocketTest::readyRead);
qDebug() << "connecting...";
socket->connectToHost("localhost", _portVal);
if (!socket->waitForConnected(10000))
{
qDebug() << "Error: " << socket->errorString();
exit(1);
}
QFile file(path);
if (file.open(QIODevice::ReadWrite))
{
// NOTE : sending the file name to the server
qDebug() << path;
socket->write(path.toStdString().c_str(), path.toStdString().size());
socket->waitForBytesWritten(3000);
socket->flush();
// NOTE : sending the file itself
QByteArray data = file.readAll();
file.close();
socket->write(data, data.length());
socket->waitForBytesWritten(3000);
socket->flush();
}
else
{
qDebug() << "not open!";
}
// sending
// socket destruction
}
void SocketTest::doConnect()
{
socket = new QTcpSocket(this);
connect(socket, &QTcpSocket::connected, this, &SocketTest::connected);
connect(socket, &QTcpSocket::disconnected, this, &SocketTest::disconnected);
connect(socket, &QTcpSocket::bytesWritten, this, &SocketTest::bytesWritten);
connect(socket, &QTcpSocket::readyRead, this, &SocketTest::readyRead);
qDebug() << "connecting...";
socket->connectToHost("localhost", _portVal);
if (!socket->waitForConnected(1000))
{
qDebug() << "Error: " << socket->errorString();
}
}
void SocketTest::connected()
{
// QFile file(_fpathStr);
// assume the directory exists and contains some files
QDir directory(_fpathStr);
QStringList files = directory.entryList(QStringList() << "*.*", QDir::Files);
for (QString filename : files)
{
sendfile(filename);
}
socket->close();
delete socket;
}
void SocketTest::disconnected()
{
qDebug() << "disconnected...";
}
void SocketTest::bytesWritten(qint64 bytes)
{
qDebug() << bytes << " bytes written...";
}
void SocketTest::readyRead()
{
qDebug() << "reading...";
qDebug() << socket->readAll();
}