-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.cpp
60 lines (57 loc) · 1.13 KB
/
server.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
59
#include "server.h"
Server::Server(QObject *parent)
: QTcpServer{parent}
{
}
//开启监听
void Server::Connect(int port)
{
listen(QHostAddress::Any,port);
}
//玩家连入(自动)
void Server::incomingConnection(qintptr handle){
socket.setSocketDescriptor(handle);
qDebug("成功连入");
connect(&socket,SIGNAL(readyRead()),this,SLOT(receiveData()));
isConnected=1;
}
//接收数据
void Server::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 Server::sendData(int x_self,int y_self,int dir_self)
{
if(isConnected){
//int[] 转 QByteArray
int self[3] = {x_self,y_self,dir_self};
QByteArray array;
array.append((char*)self, sizeof(int) * 3);
socket.write(array);
}
}
//获取数据
int Server::getX()
{
return x;
}
int Server::getY()
{
return y;
}
int Server::getDir()
{
return dir;
}