duannuo7878 2012-04-17 18:23
浏览 41
已采纳

使用PHP CLI SAPI的基于PHP的服务器

Is it possible (and practicable) to develop a (simple) server software using the PHP CLI SAPI? Is there some usable way of (real) multi-threading in PHP so the server is able to handle several request simultaneously? Or would you recommend another Scripting language for a project like this (Python or something like that)?

  • 写回答

2条回答 默认 最新

  • dpqg86714 2012-12-14 14:14
    关注

    Yes it is possible to develop a TCP or UDP server in PHP. Have a look at the set of socket functions in PHP: PHP:Sockets - Manual

    Although PHP has no multithreading capabilities you can create simple parallelism using non blocking IO (seesocket_set_nonblock()) along with socket_select()

    To answer the question 'Is it practible' it needs more information about the project requirements. Reasons for a 'yes' could be:

    • You need to integrate the server into an existing PHP class, model framework
    • You plan to interexchange PHP data structures using the server. (Might be generated using serialize(), unserialize())

    You may find additional reasons for you of course.

    Another thing you should note is that when in web server environment (e.g apache SAPI) you already have a parallel TCP server - the web server. You would just have to implement the communication between several requests. You may use PHP's IPC capabilities, a database or at least a file for that communication.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作