-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocket.php
More file actions
146 lines (110 loc) · 3.39 KB
/
Socket.php
File metadata and controls
146 lines (110 loc) · 3.39 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
<?php
/**
* Created by PhpStorm.
* User: jett
* Date: 2018/12/27 0027
* Time: 下午 02:38
*/
/**
* Class Socket
* 操作socket封装类
*
*/
class Socket {
private static $server_host;
private static $client_host;
private static $port = 27777;
private static $socket;
public function __construct() {
}
public function init() :bool {
return true;
}
static public function getServerHost() :string {
return self::$server_host;
}
static public function setServerHost(string $host) :bool {
self::$server_host = $host;
return true;
}
static public function getClientHost() :string {
return self::$client_host;
}
static public function setClientHost(string $host) :bool {
self::$client_host = $host;
return true;
}
static public function setPort(int $port) :bool {
self::$port = $port;
return true;
}
static public function getSocketObj() {
return self::$socket;
}
/**
* 检查成员变量是否满足要求
* @throws Exception
*/
static private function checkValue() {
!empty(self::$server_host) ?: self::newException("未设置服务端ip!");
set_time_limit(0);
}
/**
* 创建socket
* @throws Exception
*/
static private function createTcp(){
self::$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
!empty(self::$socket) ?: self::newException("创建socket失败!");
}
/**
* 开启socket服务端,接收并写入文件
* @return bool
* @throws Exception
*/
static public function startServer() :bool {
self::checkValue();
// 创建socket并监听
self::createTcp();
socket_bind(self::$socket, self::$server_host, self::$port) ?: self::newException("socket绑定失败!");
socket_listen(self::$socket, 5) ?: self::newException("socket监听失败");
// 循环获取接收到数据
while (true) {
// 如果获取数据失败则抛出失败信息的异常
$accept_data = socket_accept(self::$socket);
if($accept_data === FALSE){
$msg = socket_strerror(socket_last_error(self::$socket));
self::newException($msg);
}
$string = socket_read($accept_data,1024);
print_r(date("Y-m-d H:i:s")."接收到连接:\n{$string}\n");
sleep(5);
socket_write($accept_data, "已收到", strlen ("已收到"));
socket_close($accept_data);
}
return true;
}
/**
* @throws Exception
*/
static public function connectServer(){
self::checkValue();
// 创建socket
self::createTcp();
socket_connect(self::$socket, self::$server_host, self::$port) ?:
self::newException(socket_strerror(socket_last_error(self::$socket)));
// socket_connect(self::$socket, self::$server_host, self::$port);
$output="你好,服务器!";
socket_write(self::$socket, $output, strlen ($output));
$input = socket_read(self::$socket, 1024);
echo($input);
}
/**
* @param string $msg
* @param int $code
* @throws Exception
*/
static private function newException(string $msg = "", int $code = -1) {
throw new Exception($msg, $code);
}
}