资源简介
<?php class Worker{ //监听socket protected $socket = NULL; //连接事件回调 public $onConnect = NULL; //接收消息事件回调 public $onMessage = NULL; protected $workerNumber = 10; public function __construct($socket_address) { // 协议解析 if (!$res = parse_url($socket_address)) { echo "failed to parse address"; exit(); } extract($res); $protocol = SOCK_STREAM; switch ($scheme) { case 'tcp': $protocol = SOCK_STREAM; break; case 'udp': $protocol = SOCK_DRAM; break; default: # code... break; } // 初始化socket句柄 if(($sock = socket_create(AF_INET, $protocol, 0)) < 0) { echo "failed to create socket: ".socket_strerror($sock)."\n"; exit(); } if(($ret = socket_bind($sock, $host, $port)) < 0) { echo "failed to bind socket: ".socket_strerror($ret)."\n"; exit(); } if( ( $ret = socket_listen( $sock, 0 ) ) < 0 ) { echo "failed to listen to socket: ".socket_strerror($ret)."\n"; exit(); } $this->socket = $sock; } public function fork() { for ($i=0; $i < $this->workerNumber; $i ) { $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } else if ($pid == 0) { // we are the child $this->accept(); exit; } } for ($i=0; $i < $this->workerNumber; $i ) { $status=0; $pid=pcntl_wait($status); //Protect against Zombie children echo "子进程回收了:$pid".PHP_EOL; } } public function accept() { $client_id = 0; while (true) { if (($msgsock = socket_accept($this->socket)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($this->socket)) . "\n"; } else { $client_id = 1; if (is_callable($this->onConnect)) { call_user_func($this->onConnect, (int)$msgsock); } } if (false === ($buf = @socket_read($msgsock, 2048))) { echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n"; } else { if (is_callable($this->onMessage)) { call_user_func($this->onMessage, $msgsock, $buf); } } socket_close($msgsock); } } public function run() { $this->fork(); } } $worker = new Worker('tcp://0.0.0.0:9800'); $worker->onConnect = function ($fd) { echo '新的连接来了'.$fd. 'pid' . posix_getpid() . PHP_EOL; }; $worker->onMessage = function ($conn, $message) { // echo "读取到数据: {$message}"; $content = "Hello World"; $http_resonse = "HTTP/1.1 200 OK\r\n"; $http_resonse .= "Content-Type: text/html;charset=UTF-8\r\n"; $http_resonse .= "Connection: keep-alive\r\n"; $http_resonse .= "Server: php socket server\r\n"; $http_resonse .= "Content-length: ".strlen($content)."\r\n\r\n"; $http_resonse .= $content; socket_write($conn, $http_resonse, strlen($http_resonse)); }; $worker->run();
代码片段和文件信息
- 上一篇:CTF不死马源码(请勿作恶)
- 下一篇:爱搜索(多合一搜索)php源码
相关资源
- 爱搜索(多合一搜索)php源码
- CTF不死马源码(请勿作恶)
- phpStudy 探针程序源码(可探测服务器
- 将redis中的所有key 写入txt文本
- 支付宝电脑网站支付 SDK Demo(官方)
- php快速操控excel之phpspreadsheet
- socket.io的php版本(phpsocket.io)
- thinkphp5 框架执行流程
- php微擎小程序 开发
- php 知识点总结思维导图
- php获取企业微信用户信息
-
PHP获取网页ti
tle标题 - upload-lab靶场PHP源码
- ucms_1.4.8 php源码
- 微信小程序支付付款Deom(php|小程序代
- PHP图片验证码案列
- 学生信息登记表(php代码)
- php上传、编辑用户头像
- PHP许愿墙(源码+数据库)
- php查询天气预报
- 公司核名系统(php源码)
-
php excel 操作源码 xm
l - php 日历控件(DATE PICKER - JQUERY PLUGIN)
- php 日历(bootstrap-datetimepicker)
-
php将 xm
l解析成array - 实现excel数据的导入导出 thinkphp
- php判断pc端和手机端
- php上传文件
- php订单管理系统,附赠设计报告
- 微直播(php直播源码)
评论
共有 条评论