-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.cpp
58 lines (54 loc) · 1.05 KB
/
client.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "client.h"
Client::Client()
{
};
//连接
bool Client::Connect(QString ip,QString port)
{
socket.connectToHost(ip,port.toInt());
if(!socket.waitForConnected(2000)){
qDebug("连接失败");
return 0;
}
qDebug("成功连接");
connect(&socket,SIGNAL(readyRead()),this,SLOT(receiveData()));
return 1;
}
//接受数据
void Client::receiveData()
{
// QByteArray 转 int[]
QByteArray array=socket.readAll();
int data[3];
for (int i=0; i<3; i++)
{
int unTemp;
memcpy(&unTemp, array.data() + sizeof(int) * i, sizeof(int));
data[i] = unTemp;
}
x=data[0];
y=data[1];
dir=data[2];
}
//发送数据
void Client::sendData(int x_self,int y_self,int dir_self)
{
//int[] 转 QByteArray
int self[3] = {x_self,y_self,dir_self};
QByteArray array;
array.append((char*)self, sizeof(int) * 3);
socket.write(array);
}
//获取对方数据
int Client::getX()
{
return x;
}
int Client::getY()
{
return y;
}
int Client::getDir()
{
return dir;
}