-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtcpclient.cpp
More file actions
38 lines (33 loc) · 893 Bytes
/
tcpclient.cpp
File metadata and controls
38 lines (33 loc) · 893 Bytes
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
#include "tcpclient.h"
#include <QCoreApplication>
#include <QTime>
TcpClient::TcpClient(QObject *parent) : BaseParent(parent)
{
socket = new QTcpSocket(this);
connect(socket,&QTcpSocket::readyRead,this,&TcpClient::handleReadyRead);
}
bool TcpClient::Open(Para *para)
{
socket->connectToHost(para->ip, para->client_port);
QTime time;
time.start();
while((time.elapsed() < 10000) && (socket->state()!=QAbstractSocket::ConnectedState)) {
QCoreApplication::processEvents();
}
return (socket->state()==QAbstractSocket::ConnectedState);
}
void TcpClient::Close()
{
socket->disconnectFromHost();
}
void TcpClient::handleReadyRead()
{
emit sig_recv_data(socket->readAll());
}
void TcpClient::slot_send_data(QByteArray hex)
{
if(socket->state() == QAbstractSocket::ConnectedState) {
socket->write(hex);
socket->flush();
}
}