lijianjunljj 发表于 2011-11-19 21:01

php写tcp服务器怎么样?

我看了下php的pctnl库好像支持多进程而且还支持select、epoll等IO模型,不知道php做的服务端效果怎么样,有谁试过吗?

maochanglu 发表于 2011-11-20 12:29

这个不是php的强项啊。

用socket 可以写值守程序的。

lijianjunljj 发表于 2011-11-20 13:40

回复 2# maochanglu


    不见得啊 它既然有这个功能 肯定有它的存在价值 我希望能把它发挥出来

bs 发表于 2011-11-22 11:02

可以应付普通应用,php的有些网络库也引入了libevent

lijianjunljj 发表于 2011-11-22 23:54

回复 4# bs


有两问?
一、 一个php的版webgame server 可不可以fork多个进程去监听同一个端口呢,如果可以大概能撑多少个人同时在线(进程不挂掉就ok)?
二、如果采用单一进程绑定事件的方式,能撑多少?
有木试过?

bs 发表于 2011-11-23 14:42

回复bs


有两问?
一、 一个php的版webgame server 可不可以fork多个进程去监听同一个端口呢,如果 ...
lijianjunljj 发表于 2011-11-22 23:54 http://bbs.chinaunix.net/images/common/back.gif


可以fork多个进程
<?php   
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket)
throw new Exception("$errstr ($errno)<br />\n");

// 生成子进程
$pid = pcntl_fork();
if ($pid == -1) {
   die('could not fork');
} else if ($pid) {
   loop($socket , 1);
   pcntl_wait($status);
} else {
   loop($socket , 2);
}


function loop($socket , $id) {
    while ($conn = stream_socket_accept($socket)) {
            /*
            $data = '';
            while(($c=fgetc($read_sock))!==false && $c!="\0" && $c!="\n" && $c!="\r"){
                $data .= $c;
            }*/
            fwrite($conn, "Server ID :   $id , hello!\n");
            fclose($conn);
    }

    fclose($socket);
}


另外你可以试试php异步IO+消息列队+producer/consumer模型,至于可用性不敢保证,性能方面应该不会太差,之前是有PHP的web server的开源产品。

matyhtf 发表于 2014-07-11 00:08

用swoole把,C扩展实现的PHP多线程异步Server。
http://www.swoole.com/

yakczh_cu 发表于 2014-07-11 09:10

swoole vs nodejs有人测试过性能吗?

第二张皮 发表于 2014-07-11 13:49

回复 1# lijianjunljj


可以使用swoole轻松实现 http://bbs.chinaunix.net/thread-4145582-1-1.html

yakczh_cu 发表于 2014-07-12 19:35

yum 安装 swoole出错 
yum install php-pecl-swoole

Loaded plugins: fastestmirror, presto
Loading mirror speeds from cached hostfile
* base: mirrors.btte.net
* epel: ftp.jaist.ac.jp
* extras: mirrors.btte.net
* remi: rpms.famillecollet.com
* remi-test: rpms.famillecollet.com
* updates: mirrors.btte.net
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-pecl-swoole.i686 0:1.7.3-1.el6.remi.5.4 will be installed
--> Processing Dependency: php(zend-abi) = 20100525-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: php(api) = 20100412-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: php-sockets(x86-32) for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Running transaction check
---> Package php-common.i686 0:5.4.30-1.el6.remi will be installed
---> Package php-pear.noarch 1:1.9.5-0.1.el6.remi will be installed
--> Processing Dependency: php-xml for package: 1:php-pear-1.9.5-0.1.el6.remi.noarch
--> Processing Dependency: php-posix for package: 1:php-pear-1.9.5-0.1.el6.remi.noarch
--> Processing Dependency: php-cli for package: 1:php-pear-1.9.5-0.1.el6.remi.noarch
--> Running transaction check
---> Package php-cli.i686 0:5.5.15-0.1.RC1.el6.remi will be installed
--> Processing Dependency: php-common(x86-32) = 5.5.15-0.1.RC1.el6.remi for package: php-cli-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: libssl.so.10(libssl.so.10) for package: php-cli-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: libcrypto.so.10(libcrypto.so.10) for package: php-cli-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: libcrypto.so.10(OPENSSL_1.0.1_EC) for package: php-cli-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: libcrypto.so.10(OPENSSL_1.0.1) for package: php-cli-5.5.15-0.1.RC1.el6.remi.i686
---> Package php-process.i686 0:5.5.15-0.1.RC1.el6.remi will be installed
---> Package php-xml.i686 0:5.5.15-0.1.RC1.el6.remi will be installed
--> Running transaction check
---> Package openssl.i686 0:1.0.0-20.el6_2.5 will be updated
---> Package openssl.i686 0:1.0.1e-16.el6_5.14 will be an update
---> Package php-common.i686 0:5.4.30-1.el6.remi will be installed
--> Processing Dependency: php-pecl-zip(x86-32) for package: php-common-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: php(zend-abi) = 20100525-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: php(api) = 20100412-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
---> Package php-common.i686 0:5.5.15-0.1.RC1.el6.remi will be installed
--> Processing Dependency: php-pecl-zip(x86-32) for package: php-common-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: php-pecl-jsonc(x86-32) for package: php-common-5.5.15-0.1.RC1.el6.remi.i686
--> Running transaction check
---> Package php-common.i686 0:5.4.30-1.el6.remi will be installed
--> Processing Dependency: php-pecl-zip(x86-32) for package: php-common-5.5.15-0.1.RC1.el6.remi.i686
--> Processing Dependency: php(zend-abi) = 20100525-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: php(zend-abi) = 20100525-x86-32 for package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686
--> Processing Dependency: php(api) = 20100412-x86-32 for package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686
--> Processing Dependency: php(api) = 20100412-x86-32 for package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686
---> Package php-common.i686 0:5.5.15-0.1.RC1.el6.remi will be installed
--> Processing Dependency: php-pecl-zip(x86-32) for package: php-common-5.5.15-0.1.RC1.el6.remi.i686
---> Package php-pecl-jsonc.i686 0:1.3.5-1.el6.remi.5.4 will be installed
--> Processing Dependency: php(zend-abi) = 20100525-x86-32 for package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686
--> Processing Dependency: php(api) = 20100412-x86-32 for package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686
--> Finished Dependency Resolution
Error: Package: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
         Requires: php-pecl-zip(x86-32)
         Available: php-common-5.4.29-3.el6.remi.i686 (remi)
               php-pecl-zip(x86-32) = 1.11.0
         Available: php-common-5.4.30-1.el6.remi.i686 (remi)
               php-pecl-zip(x86-32) = 1.11.0
         Available: php-common-5.3.3-26.el6.i686 (base)
               Not found
         Available: php-common-5.3.3-27.el6_5.i686 (updates)
               Not found
         Installing: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
               Not found
Error: Package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686 (remi)
         Requires: php(zend-abi) = 20100525-x86-32
         Available: php-common-5.3.3-26.el6.i686 (base)
               php(zend-abi) = 20090626
         Available: php-common-5.3.3-27.el6_5.i686 (updates)
               php(zend-abi) = 20090626
         Available: php-common-5.4.29-3.el6.remi.i686 (remi)
               php(zend-abi) = 20100525-x86-32
         Available: php-common-5.4.30-1.el6.remi.i686 (remi)
               php(zend-abi) = 20100525-x86-32
         Installing: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
               php(zend-abi) = 20121212-32
Error: Package: php-pecl-swoole-1.7.3-1.el6.remi.5.4.i686 (remi)
         Requires: php(api) = 20100412-x86-32
         Available: php-common-5.3.3-26.el6.i686 (base)
               php(api) = 20090626
         Available: php-common-5.3.3-27.el6_5.i686 (updates)
               php(api) = 20090626
         Available: php-common-5.4.29-3.el6.remi.i686 (remi)
               php(api) = 20100412-x86-32
         Available: php-common-5.4.30-1.el6.remi.i686 (remi)
               php(api) = 20100412-x86-32
         Installing: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
               php(api) = 20121113-32
Error: Package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686 (remi)
         Requires: php(zend-abi) = 20100525-x86-32
         Available: php-common-5.3.3-26.el6.i686 (base)
               php(zend-abi) = 20090626
         Available: php-common-5.3.3-27.el6_5.i686 (updates)
               php(zend-abi) = 20090626
         Available: php-common-5.4.29-3.el6.remi.i686 (remi)
               php(zend-abi) = 20100525-x86-32
         Available: php-common-5.4.30-1.el6.remi.i686 (remi)
               php(zend-abi) = 20100525-x86-32
         Installing: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
               php(zend-abi) = 20121212-32
Error: Package: php-pecl-jsonc-1.3.5-1.el6.remi.5.4.i686 (remi)
         Requires: php(api) = 20100412-x86-32
         Available: php-common-5.3.3-26.el6.i686 (base)
               php(api) = 20090626
         Available: php-common-5.3.3-27.el6_5.i686 (updates)
               php(api) = 20090626
         Available: php-common-5.4.29-3.el6.remi.i686 (remi)
               php(api) = 20100412-x86-32
         Available: php-common-5.4.30-1.el6.remi.i686 (remi)
               php(api) = 20100412-x86-32
         Installing: php-common-5.5.15-0.1.RC1.el6.remi.i686 (remi-test)
               php(api) = 20121113-32
swoole是不是对对php版本有要求,这现在的是php5.4
页: [1] 2
查看完整版本: php写tcp服务器怎么样?