blob: b2aefe3da87c9aeb28ad260bf6003faecdc315c0 (
plain)
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
|
/*
* udpClient.cpp
*
* Created on: Apr 6, 2011
* Author: blacky
*/
#include "tcpClient.h"
#include <iostream>
using namespace std;
tcpClient::tcpClient(QString serverIP_, int serverPort_) {
serverIP=serverIP_;
serverPort=serverPort_;
QObject::connect(this, SIGNAL(connected()), this, SLOT(sendOut()));
}
tcpClient::~tcpClient() {
// TODO Auto-generated destructor stub
}
int tcpClient::connect(int source, int sink) {
sendBuffer.clear();
sendBuffer.append(QByteArray::number(MSG_CONNECT));
sendBuffer.append('#');
sendBuffer.append(QByteArray::number(source));
sendBuffer.append('#');
sendBuffer.append(QByteArray::number(sink));
if (send()) {
this->waitForReadyRead(CONNECT_TIMEOUT);
return this->readAll().toInt();
} else {
return -1;
}
}
void tcpClient::system_ready() {
sendBuffer.clear();
sendBuffer.append(QByteArray::number(SIG_system_ready));
send();
}
bool tcpClient::send() {
QHostAddress adr;
adr.setAddress(serverIP);
this->connectToHost(adr,serverPort);
if (!this->waitForConnected(CONNECT_TIMEOUT)) {
cout<<"No connection"<<endl;
return false;
} else {
cout<<"got connection"<<endl;
return true;
}
}
void tcpClient::sendOut() {
this->write(sendBuffer);
}
|