PHP 处理多个 TCP 连接的能力? [英] PHP capability to handle multiple TCP connections?

查看:38
本文介绍了PHP 处理多个 TCP 连接的能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 PHP 是否可以通过 TCP 端口充当服务器?1GB RAM 服务器可以处理多少个空闲连接?PHP 是否可以接收到一个 TCP 端口的多个连接(就像 apache 在端口 80 上所做的那样?)

I was wondering if PHP can act as a server over a TCP port? How many IDLE connections can a 1GB RAM server handle? Can PHP receive many connections to one TCP port (like apache does on port 80?)

如果我有一个客户端连接到 PHP 打开的套接字,我如何向它发送数据?(例如a.php正在监听5000端口,有客户端连接,稍后b.php需要向那个客户端发送一些东西,推送消息?)

If i have a client connected to the socket opened by PHP, how can I send data to it? (for example a.php is listening on port 5000, some client is connected, later on b.php needs to send something to that client, well push a message?)

或者我应该改用 UDP 吗?

Or shall i use UDP instead?

推荐答案

朋友,我的第一个建议是,即使 PHP 本身可以做到这一点.PHP 不是实现 TCP 套接字服务器的最佳技术.原因是因为记得php每个脚本都有 max_execution_time 并使服务器永远运行将需要将此值设置为 0,即永远.1gb ram 服务器可以处理的连接数取决于每个 php 进程消耗多少 ram 和处理器.

Friend, my first advice is that even if PHP can do this natively. PHP is not the best technology to implement a TCP Sockets Server. The reason is because remember php has max_execution_time for each script and make a server run forever will require set this value to 0 wich means forever. The amount of connections of 1gb of ram server can handle depends on how much ram and processor consume each php process.

但是无论如何,如果您决定使用 php.是的,您可以使用 php 构建一个异步 tcp 套接字服务器.

But anyway, if you decide to do it using php. Yes, you can build with php an asynchronous tcp socket server.

只需创建一个主套接字监听 5000(例如).对于与此主套接字连接的每个客户端套接字,然后为每个传入连接创建一个带有随机空闲端口(由操作系统选择)的子套接字,您可以在释放主套接字的同时开始与子套接字通信以继续侦听另一个传入连接.

Just create a master socket listening 5000(for example). And for each client sockets that connects with this master socket then create a child socket with a random free port(selected by the os) for each incoming connection and you can start talking with the child socket while releasing the master to continue listening for another incoming connections.

这篇关于PHP 处理多个 TCP 连接的能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆