/
socket.php
61 lines (50 loc) · 1.13 KB
/
socket.php
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
<?php
/////////php Code///////
class Socket{
public $socket;
public $host ;
public $port;
public function __construct($host,$port){
$this->host = $host;
$this->port = $port;
}
public function init()
{
$this->socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
}
public function open_socket()
{
$result = socket_connect($this->socket,$this->host,$this->port);
return $result;
}
public function getStatus()
{
try
{
$status = socket_get_status($this->socket);
if ($status['timed_out']) {
return FALSE;
}
if ($status=="")
{
return TRUE;
}
}
catch (Exception $e) {
echo $e->getMessage();
}
}
public function send_data($content){
socket_write($this->socket,$content,strlen($content));
}
public function read_data(){
while($buf = socket_read($this->socket, 1024))
if($buf = trim($buf))
break;
return $buf;
}
public function close_socket(){
socket_close($this->socket);
}
}
?>