-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathip_connection.cpp
More file actions
92 lines (79 loc) · 2.02 KB
/
ip_connection.cpp
File metadata and controls
92 lines (79 loc) · 2.02 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
#include "ip_connection.h"
IP_Connection::IP_Connection(QObject *parent) :
QObject(parent)
{
IP_Init();
connect(socket, SIGNAL(connected()), this, SIGNAL(Connected()));
connect(socket, SIGNAL(disconnected()), this, SIGNAL(Disconnected()));
connect(socket, SIGNAL(disconnected()), this, SLOT(sdisconnected()));
connect(socket, SIGNAL(readyRead()), this, SIGNAL(dataReceived()));
}
IP_Connection::~IP_Connection()
{
delete socket;
}
void IP_Connection::IP_Init()
{
socket = new QTcpSocket(this);
port = 80;
Delay_waitForConnected = 1000;
Delay_waitForBytesWritten = 1000;
Delay_waitForReadyRead = 150;
_Connected = false;
}
bool IP_Connection::PCB_Connect()
{
socket->connectToHost(IP_Address, 80);
_Connected = socket->waitForConnected(Delay_waitForConnected);
return _Connected;
}
void IP_Connection::PCB_Disconnect()
{
if (_Connected) {
socket->disconnectFromHost();
_Connected = false;
}
}
void IP_Connection::PCB_SendData(QString message)
{
if (_Connected) {
socket->write(message.toUtf8().constData());
socket->waitForBytesWritten(Delay_waitForBytesWritten);
} else {
qDebug() << "Connection is not established! Cannot send the data.";
}
}
QByteArray IP_Connection::PCB_ReceiveData()
{
QByteArray socketResponse;
if (_Connected) {
socket->waitForReadyRead(Delay_waitForReadyRead);
socketResponse = socket->readAll();
} else {
qDebug() << "Connection is not established! Cannot receive the data.";
}
return socketResponse;
}
void IP_Connection::SetIPAddress(QString adr)
{
IP_Address = adr;
}
bool IP_Connection::IsConnected()
{
return _Connected;
}
//void IP_Connection::readyRead()
//{
//// qDebug() << "Some data has been received.";
// QByteArray response;
// response = socket->readAll();
// qDebug() << response << endl;
//}
void IP_Connection::sdisconnected()
{
_Connected = false;
}
QByteArray IP_Connection::readAll()
{
return socket->readAll();
}