-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacketnetwork.cpp
42 lines (40 loc) · 1.09 KB
/
packetnetwork.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
#include "packetnetwork.h"
#include <QHostAddress>
PacketNetwork::PacketNetwork()
{
}
void PacketNetwork::setPacket(Packet *sendPacket)
{
this->sendPacket = sendPacket;
this->data = sendPacket->getByteArray();
}
bool PacketNetwork::init()
{
socket = new QTcpSocket();
socket->connectToHost(QHostAddress(this->sendPacket->toIP), this->sendPacket->port, QTcpSocket::ReadWrite);
// socket->connectToHost(QHostAddress::LocalHost, 8081);
connect(socket, SIGNAL(connected()), this, SLOT(send()));
return socket->waitForConnected();
}
bool PacketNetwork::send()
{
qDebug() << "send";
if(socket->isOpen())
{
// socket->write(IntToArray(data.size())); //write size of data
qDebug() << data;
socket->write(data); //write the data itself
return socket->waitForBytesWritten();
}
else
return false;
}
void PacketNetwork::packetToSend(Packet *sendPacket)
{
setPacket(sendPacket);
// 如果連接失敗回傳 false
qDebug() << init();
socket->close();
delete socket;
qDebug() << "close";
}