PHP5中可以使用新增的stream_socket_client()函数直接替换掉fsocketopen()。PHP5之前的版本,你需要自己动手,用sockets扩展解决问题。PHP5的先进之处在于,你可以用stream_select()处理几乎所有的stream-例如你可以通过include STDIN用它接收键盘输入并保存进数组,你还可以接收通过proc_open()打开的管道中的数据。

下面来分享一个PHP多线程类

代码如下
* @title:      PHP多线程类(Thread)
* @version:    1.0
* @author:     phper.org.cn < web@phper.org.cn >
* @published:  2010-11-2
*
* PHP多线程应用示例:
*  require_once 'thread.class.php';
*  $thread = new thread();
*  $thread->addthread('action_log','a');
*  $thread->addthread('action_log','b');
*  $thread->addthread('action_log','c');
*  $thread->runthread();

*  function action_log($info) {
*      $log = 'log/' . microtime() . '.log';
*      $txt = $info . "rnrn" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn";
*      $fp = fopen($log, 'w');
*      fwrite($fp, $txt);
*      fclose($fp);
*  }
*/
class thread {
    var $hooks = array();
    var $args = array();
    function thread() {
    }
    function addthread($func)
    {
        $args = array_slice(func_get_args(), 1);
        $this->hooks[] = $func;
        $this->args[] = $args;
        return true;
    }
    function runthread()
    {
        if(isset($_GET['flag']))
        {
            $flag = intval($_GET['flag']);
        }
        if($flag || $flag === 0)
        {
            call_user_func_array($this->hooks[$flag], $this->args[$flag]);
        }
        else
        {
            for($i = 0, $size = count($this->hooks); $i < $size; $i++)
            {
                $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);
                if($fp)
                {
                    $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn";
                    $out .= "Host: {$_SERVER['HTTP_HOST']}rn";
                    $out .= "Connection: Closernrn";
                    fputs($fp,$out);
                    fclose($fp);
                }
            }
        }
    }
}

最新文章

  1. windows下配置apache+php环境
  2. Linux笔记之——Linux关机命令详解(转)
  3. 当shiro不进入自定义realm的权限认证方法时
  4. 【CodeForces 699D】Fix a Tree
  5. iOS常用设计模式和机制之代理
  6. SQL语句,给自己的记录
  7. Modbus Poll master-slave测试 Dtech USB转485(worldsing 笔记)
  8. WEB 开发异常:java.lang.ClassNotFoundException
  9. 【Python开发实战】Windows7+VirtualBox+Ubuntu环境配置
  10. [C++程序设计]返回指针值的函数
  11. iOS APP跳转设置界面以及设置中的其他界面
  12. 使用Phar来打包发布PHP程序
  13. python安装mysqlclient模块报fatal error: Python.h:解决方法
  14. python自学——文件处理(强制刷新)
  15. 【BLE】CC2541之加入自己定义任务
  16. [POI2000]Repetitions
  17. STL - 容器 - Set
  18. jQuery异步Deferred
  19. POJ3292 Semi-prime H-numbers [数论,素数筛]
  20. 转Web开发的发展史---Web开发技术的演变

热门文章

  1. 时间的获取和转换,C#和Sql
  2. HDU 1194 - Beat the Spread!
  3. API 之 MessageBox
  4. 使用HTML5的页面资源预加载(Link prefetch)功能加速你的页面加载速度
  5. 小记:对Android网络下载工具的初步封装!(包括json,字符串下载(volley),和图片下载(glide))
  6. NFC的安全性
  7. iis 回收工作进程时出错的解决办法
  8. 改变DM6467的内存划分
  9. Python学习笔记10-Python MysqlHelper ,MySql 辅助类
  10. cocos2d js 怎样动态载入外部图片