forked from domingosliraneto/BibliotecaMatematica
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
109 lines (88 loc) · 2.76 KB
/
client.cpp
File metadata and controls
109 lines (88 loc) · 2.76 KB
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
#include "client.h"
#include <iostream>
//START PUBLIC-----///////
Client::Client(QObject *parent): QObject(parent) {
registerSlots();
Init();
}
Client::Client(QString address, quint16 port) {
registerSlots();
Init();
Connect2Host(address, port);
}
Client::~Client() {
DisconnectFromHost();
}
void Client::Connect2Host(QString address, quint16 port) {
QHostAddress addr(address);
socket.connectToHost(addr, port);
}
void Client::DisconnectFromHost() {
socket.disconnectFromHost();
socket.close();
}
//QTcpClient Client::getClient() {
// return this->Client;
//}
bool Client::isConneted() {
std::cout<<socket.errorString().toStdString();
return this->hasConnection;
}
bool Client::writeData(QString str)
{
if(this->hasConnection)
{
socket.waitForConnected();
socket.write(str.toStdString().c_str());
}
return socket.flush();
}
QString Client::dataReceived()
{
return this->bytesReaded;
}
//END PUBLIC------////////
//START PRIVATE-----/////////
void Client::Init() {
QNetworkConfigurationManager manager;
if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {
// Get saved network configuration
QSettings settings(QSettings::UserScope, QLatin1String("QtProject"));
settings.beginGroup(QLatin1String("QtNetwork"));
const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
settings.endGroup();
// If the saved network configuration is not currently discovered use the system default
QNetworkConfiguration config = manager.configurationFromIdentifier(id);
if ((config.state() & QNetworkConfiguration::Discovered) !=
QNetworkConfiguration::Discovered) {
config = manager.defaultConfiguration();
}
this->networkSession = new QNetworkSession(config, this);
connect(this->networkSession, SIGNAL(opened()), this, SLOT(sessionOpened()));
this->networkSession->open();
}
}
void Client::registerSlots() {
connect(&socket, SIGNAL(connected()), this, SLOT(Awake()));
connect(&socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
//END PRIVATE-----///////////
//START SLOTS-----////////
void Client::Awake() {
this->hasConnection = true;
emit connectionSuccessful();
}
/*!
* \brief Client::readData
* \details This function is triggered when the Client receives any data.
*/
void Client::readData(){
char buffer[1024] = {0};
unsigned sizeChar = socket.bytesAvailable();
socket.read(buffer, socket.bytesAvailable());
this->bytesReaded.clear();
for(unsigned i = 0; i < sizeChar; ++i)
this->bytesReaded.append(buffer[i]);
emit this->hasReadData();
}
//END SLOTS-----//////////