-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrold.php
More file actions
255 lines (224 loc) · 6.32 KB
/
strold.php
File metadata and controls
255 lines (224 loc) · 6.32 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
//include("read.php");
// prevent the server from timing out
//set_time_limit(5);
// $bin = pack("a3", "中");
// echo "output: " . $bin . "\n";
// echo "output: 0x" . bin2hex($bin) . "\n"; 16进制
// echo "output: " . chr(0xe4) . chr(0xb8) . chr(0xad) . "\n";
// echo "output: " . $bin{0} . $bin{1} . $bin{2} . "\n";
$ps = new PS();
$ps->connect();
// if ($err = socket_last_error($this->socket))
// {
// socket_close($this->socket);socket_recv($this->socket,$buffer,2048,0);
// die(socket_strerror($err) . "\n");
// }
class PS {
const MSG_INVALID = 0;
const MSG_CONNECT = 1;
const MSG_CONNECTED = 2;
const MSG_AUTH = 3;
const MSG_AUTHED = 4;
const MSG_INFO = 5;
const MSG_INFO_RET = 6;
const MSG_USER = 7;
const HEAD_SIZE = 28;
public $sockett;
public $socket = array();
public $host = "192.168.1.163";
public $port = "41000";
function __construct(){
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or die("Unable to create socket\n");
//$this->socket[] = $this->socket;
$result = socket_connect($this->socket, $this->host, $this->port);
if($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($this->socket)) . "\n";
} else {
echo "Connect OK \n";
}
// do{
// echo 'test'.time().'<br/>';
// $this->welive();
// set_time_limit(20);
//sleep(5);
// }while(true);
}
function welive() {
echo "welive";
//sleep(5);
}
//组包头
function packhead($message,$id,$content = "") {
//$content = "";
$Stx = pack("c", 0x27);
$LineServerId = pack("c", 1);
$DestServerType = pack("c", 2);
$Ckx = pack("c", 0x72);
$Message = pack("V", $message);
$SrcZoneId = pack("V", 3);
$DestZoneId = pack("V", 0);
$Id = pack("l", $id);
$packetsize = strlen($content);
$RSV = pack("l",$packetsize);
$PacketSizee = pack("V", $packetsize);
$binarydata = $Stx.$LineServerId.$DestServerType.$Ckx.$Message.$SrcZoneId.$DestZoneId.$Id.$RSV.$PacketSizee.$content;
return $binarydata;
}
//解包头
function unpackhead($bytes) {
$Head = unpack("c1Stx/c1LineServerId/c1DestServerType/c1Ckx/V1Message/V1SrcZoneId/V1DestZoneId/l1Id/l1RSV/V1PacketSizee", $bytes);
return $Head;
}
//解包体
function unpackcontent($bytes) {
$mcontent= substr($bytes,28,4);
$socketid = unpack("l",$mcontent);
return $socketid;
}
function send($binarydata) {
$len = socket_write ($this->socket , $binarydata, strlen($binarydata));
$bytes = socket_read($this->socket,4096);
return $bytes;
}
function read($binarydata) {
$bytes = socket_read($this->socket,4096);
return $bytes;
}
//获取服务器返回状态
function status($mesg){
switch($mesg){
case self::MSG_CONNECTED:
return 2;
break;
case self::MSG_AUTH:
//$socketid = unpack("l",$mcontent); 3
return 3;
break;
case self::MSG_AUTHED:
return 4;
break;
case self::MSG_INFO:
return 5;
break;
case self::MSG_INFO_RET:
return 6;
break;
case self::MSG_USER:
return 7;
break;
default:
return false;
}
}
function connect(){
//首次连接
$binarydata = $this->packhead(self::MSG_CONNECT,1509171435);
$len = socket_write($this->socket , $binarydata, strlen($binarydata));
$bytes = socket_read($this->socket,4096);
$Head = $this->unpackhead($bytes);
$mcontent = substr($bytes,28,4);
print_r($Head);
if($this->status($Head['Message']) == 2) {
$this->connected($mcontent);
} else {
return false;
}
}
function connected($mcontent){
$content='';
$socketid = unpack("l",$mcontent);
if($socketid){
$contentt = pack("l", $socketid[1]);
$contenttt = pack("c", 1);
$content = $contentt.$content;
$binarydata = $this->packhead(self::MSG_AUTH,0,$content);
socket_write($this->socket , $binarydata, strlen($binarydata));
$bytess = socket_read($this->socket,4096);
//$bytess = send($binarydata);
$Headd = $this->unpackhead($bytess);
print_r($Headd);
if($this->status($Headd['Message']) == 4) {
//print_r($mcontentt);
$this->authed();
} else {
return false;
}
}
}
function authed(){
$contentt = pack("l", 1111);
$contenttt = pack("c", 111);
$content = $contentt.$contenttt;
$binarydata = $this->packhead(233,0,$content);
socket_write($this->socket , $binarydata, strlen($binarydata));
print_r(5);
// $bytess = socket_read($this->socket,4096);
// $Headd = $this->unpackhead($bytess);
// if($this->status($Headd['Message']) == 5) {
// print_r(5);
// $this->info($Headd,$mcontentt);
// } else {
// return false;
// }
}
function info($mcontent){
$content='';
$socketid = unpack("l",$mcontent);
if($socketid){
$contentt = pack("l", $socketid[1]);
$contenttt = pack("c", 1);
$content = $contentt.$content;
$binarydata = $this->packhead(self::MSG_INFO_RET,0,$content);
socket_write($this->socket , $binarydata, strlen($binarydata));
$bytess = socket_read($this->socket,4096);
$Headd = $this->unpackhead($bytess);
$mcontentt= substr($bytess,28,4);
if($this->status($Headd['Message']) == 5) {
$this->inforet($Headd,$mcontentt);
} else {
return false;
}
}
}
function inforet($mcontent){
$content='';
$socketid = unpack("l",$mcontent);
if($socketid){
$contentt = pack("l", $socketid[1]);
$contenttt = pack("c", 1);
$content = $contentt.$content;
$binarydata = $this->packhead(self::MSG_INFO_RET,0,$content);
socket_write($this->socket , $binarydata, strlen($binarydata));
$bytess = socket_read($this->socket,4096);
$Headd = $this->unpackhead($bytess);
$mcontentt= substr($bytess,28,4);
if($this->status($Headd['Message']) == 6) {
$this->user($Headd,$mcontentt);
} else {
return false;
}
}
}
function user($mcontent){
$content='';
$socketid = unpack("l",$mcontent);
if($socketid){
$contentt = pack("l", $socketid[1]);
$contenttt = pack("c", 1);
$content = $contentt.$content;
$binarydata = $this->packhead(self::MSG_USER,0,$content);
socket_write($this->socket , $binarydata, strlen($binarydata));
$bytess = socket_read($this->socket,4096);
$Headd = $this->unpackhead($bytess);
$mcontentt= substr($bytess,28,4);
if($this->status($Headd['Message']) == 7) {
return $mcontentt ;
} else {
return false;
}
}
}
}
?>