php实现的简单多进程服务器类完整示例

2025-05-27 0 84

本文实例讲述了php实现的简单多进程服务器类。分享给大家供大家参考,具体如下:

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
<?php

class server

{

public $port;

public $ip;

protected $server;

public function __construct($ip = '0.0.0.0', $port)

{

$this->ip = $ip;

$this->port = $port;

$this->createSocket(); //创建一个通讯节点

}

public function listen($callback)

{

if(!is_callable($callback)){

throw new Exception('不是闭包,请传递正确的参数');

}

//只要我们接收到客户端的数据,就fork一个子进程处理

while ($client = socket_accept($this->server)) { //等待客户端接入,返回的是客户端的连接

$buf = socket_read($client, 1024); //读取客户端内容

$pid=pcntl_fork(); //创建子进程

//父进程和子进程都会执行下面代码

if ($pid == -1) {

//错误处理:创建子进程失败时返回-1.

die('could not fork');

} else if ($pid) {

//父进程会得到子进程号,所以这里是父进程执行的逻辑

var_dump('父进程',$pid);

pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。

} else {

//子进程得到的$pid为0, 所以这里是子进程执行的逻辑。

//睡眠

if($this->checkRule("/sleep/i",$buf)){

sleep(10);

$this->response('休眠10S',$client);

socket_close($client);

return;

}

//请求过滤

if(empty($this->checkRule("/GET\\s(.*?)\\sHTTP\\/1.1/i",$buf))){

socket_close($client);

return;

}

//响应

$response= call_user_func($callback,$buf); //回调$callback函数

$this->response($response,$client);

usleep(1000); //微妙为单位,1000000 微妙等于1秒

socket_close($client);

exit(); //直接退出

}

}

// while (true) {

// $client = socket_accept($this->server); //等待客户端接入,返回的是客户端的连接

// $buf = socket_read($client, 1024); //读取客户端内容

//

// //睡眠

// if($this->checkRule("/sleep/i",$buf)){

// sleep(10);

// $this->response('休眠10S',$client);

// socket_close($client);

// return;

// }

// //请求过滤

// if(empty($this->checkRule("/GET\\s(.*?)\\sHTTP\\/1.1/i",$buf))){

// socket_close($client);

// return;

// }

//

// //响应

// $response= call_user_func($callback,$buf); //回调$callback函数

// $this->response($response,$client);

// usleep(1000); //微妙为单位,1000000 微妙等于1秒

// socket_close($client);

//

// }

socket_close($this->server);

}

//io 复用

//epoll 模型

//多进程

protected function createSocket()

{

$this->server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

//bind

socket_set_option($this->server, SOL_SOCKET, SO_REUSEADDR, 1); //复用还处于 TIME_WAIT

socket_bind($this->server, $this->ip, $this->port); //细节性的处理自行完成

socket_listen($this->server); //开始监听

}

/**

* 协议过滤

* @param $reg

* @param $buf

* @return mixed

*/

protected function checkRule($reg,$buf){

if(preg_match($reg,$buf,$matchs)){

return $matchs;

}

return false;

}

//请求处理类

public function request($buf){

//1.只允许http协议访问

// if(preg_match("GET\\s(.*?)\\sHTTP/1.1",$buf,$matchs)){ //匹配到http协议

// return true;

// }else{

// return false;

// }

//2.过滤掉/favicon.ico

//3.获取请求信息

}

protected function response($content,$client){

//返回数据给客户端,响应处理

$string="HTTP/1.1 200 OK\\r\\n";

$string.="Content-Type: text/html;charset=utf-8\\r\\n";

$string.="Content-Length: ".strlen($content)."\\r\\n\\r\\n";

socket_write($client,$string.$content);

}

}

希望本文所述对大家PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/qq_22640823/article/details/103468715

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php实现的简单多进程服务器类完整示例 https://www.kuaiidc.com/70454.html

相关文章

发表评论
暂无评论