-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSocket.php
191 lines (167 loc) · 3.78 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
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
<?php
/**
* @author Ewan
* @email [email protected]
* The more effort ,the more lucky
* .--.
* |o_o |
* |:_/ |
* // \ \
* (| | )
* /'\_ _/`\
* \___)=(___/
* @time: 16/12/16 下午5:09
* @version 1.0
* @package Socket
*/
namespace Socket;
class Socket
{
/**
* IP地址,主机host 127.0.0.1/localhost ...
*
* @var string
*/
protected $host = '';
/**
* 端口号 8080 ...
*
* @var integer
*/
protected $port = 0;
/**
* Socket
*
* @var resource
*/
protected $socket = null;
/**
* 通讯协议
*
* @var integer
*/
protected $domain = AF_INET;
/**
* Socket类型
*
* @var integer
*/
protected $type = SOCK_STREAM;
/**
* 是设置Socket指定通讯协议下的具体协议
*
* @var integer
*/
protected $protocol = SOL_TCP;
/**
* 构造函数
*
* @param string $host 类似localhost,127.0.0.1或者其它IP
* @param integer $port 8080或者其它不被占用的端口
*/
public function __construct($host, $port)
{
}
/**
* 写入Socket
*
* @param string $str 发送给服务端/客户端
* @return integer
*/
public function write($resource, $str)
{
// 显示输出给客户端
return (int)socket_write($resource, $str);
}
/**
* 从客户端读取
* 阻塞,直到接到客户端数据
*
* @param resource $socket
* @return string
*/
public function _read($socket)
{
$ret_str = '';
$chunk_size = 1024;
// 读取输入直到结束...
do {
$read = socket_read($socket, $chunk_size);
if ($read !== false) {
$ret_str .= $read;
}
} while (($read !== false) && (mb_strlen($read) >= $chunk_size));
return $ret_str;
}
/**
* 创建 socket
*/
protected function create($host, $port)
{
if (empty($host)) {
throw new \InvalidArgumentException('$host was empty');
}
if (empty($port)) {
throw new \InvalidArgumentException('$port was empty');
}
$this->socket = socket_create($this->domain, $this->type, $this->protocol);
if (empty($this->socket)) {
$this->throwError();
}
// 验证主机IP...
if (!preg_match('#^\d+(\.\d+)+$#', $host)) {
$ip = gethostbyname($host);
if ($ip === $host) {
$this->throwError(sprintf('Host %s could not be converted to IP address', $host));
} else {
$host = $ip;
}
}
$this->host = $host;
$this->port = $port;
}
/**
* 错误捕捉
*
* @param string $errmsg
* @param resource $socket
* @throws \UnexpectedValueException
*/
protected function throwError($errmsg = '', $socket = null)
{
if (!empty($errmsg)) {
$errmsg .= '. Socket error: ';
}
throw new \UnexpectedValueException(
sprintf(
'%s"%s"',
$errmsg,
$this->getError($socket)
)
);
}
/**
* 获取错误信息
*
* @param resource $socket
* @return string
*/
protected function getError($socket = null)
{
if (empty($socket)) {
$socket = $this->socket;
}
return socket_strerror(socket_last_error($socket));
}
/**
* 析构函数
* 基本上,关闭所有打开的Socket
*/
public function __destruct()
{
if (!empty($this->con)) {
socket_close($this->con);
}
socket_close($this->socket);
}
}