<?php

 //Accpet the http client request and generate response content.
//As a demo, this function just send "PHP HTTP Server" to client.
function handle_http_request($address, $port)
{
$max_backlog = 16;
$res_content = "HTTP/1.1 200 OK
Content-Length: 15
Content-Type: text/plain; charset=UTF-8 PHP HTTP Server";
$res_len = strlen($res_content); //Create, bind and listen to socket
if(($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE)
{
echo "Create socket failed!\n";
exit;
} if((socket_bind($socket, $address, $port)) === FALSE)
{
echo "Bind socket failed!\n";
exit;
} if((socket_listen($socket, $max_backlog)) === FALSE)
{
echo "Listen to socket failed!\n";
exit;
} //Loop
while(TRUE)
{
if(($accept_socket = socket_accept($socket)) === FALSE)
{
continue;
}
else
{
socket_write($accept_socket, $res_content, $res_len);
socket_close($accept_socket);
}
}
} //Run as daemon process.
function run()
{
if(($pid1 = pcntl_fork()) === 0)
//First child process
{
posix_setsid(); //Set first child process as the session leader. if(($pid2 = pcntl_fork()) === 0)
//Second child process, which run as daemon.
{
//Replaced with your own domain or address.
handle_http_request('www.codinglabs.org', 9999);
}
else
{
//First child process exit;
exit;
}
}
else
{
//Wait for first child process exit;
pcntl_wait($status);
}
} //Entry point.
run(); ?>

最新文章

  1. STM32 NVIC配置详解
  2. 将PDM文件导出成CHM帮助文件
  3. lipo命令
  4. phpstorm使用技巧
  5. 软件版本中的Alpha,Beta,RC,Trial是什么意思?
  6. spring sts 从数据库中反向生成实体类
  7. &lt;一年成为Emacs高手&gt;更新到20130706版
  8. Eclipse中集成Perforce插件
  9. Robot Framework中经常用的第三方库的安装方法
  10. linux 操作系统/xxx目录下都是什么文件?
  11. iOS开发所有KeyboardType与图片对应展示
  12. Windows10文件目录下添加 Shift+右键打开管理员Powershell窗口
  13. c++ 之模板进阶
  14. 从PRISM开始学WPF(番外)共享上下文 RegionContext-更新至Prism7.1
  15. activemq配置安装
  16. 分组PARTITION BY及游标CURSOR的用法
  17. three.js中的文字
  18. JS事件(五)内存与性能
  19. vue cli3.0 结合echarts3.0和地图的使用方法
  20. IDC:IDC(互联网数据中心)

热门文章

  1. 数据结构11: 栈(Stack)的概念和应用及C语言实现
  2. kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU3336 Count the string
  3. JIRA安装部署说明
  4. office word使用技巧汇总
  5. java 获取 正在执行的方法名
  6. django 异常问题总结
  7. git学习--clone和pull
  8. P3225 [HNOI2012]矿场搭建
  9. appium ios 真机自动化环境搭建
  10. mgo02_mongodb启动警告处理