dtrvzd1171 2015-08-25 16:14
浏览 38

生成套接字服务器,返回端口,并继续运行

I'm looking to have a web application spawn a socket server thread in the background. Each PHP session gets its own thread, and there can be any number of threads.

I bind the socket using a port of zero, so that it picks a random open port. My socket server starts correctly, and moves into it's listening loop without issue. The issue I have, however, is that my PHP script that starts the thread using shell_exec() needs to get a response from the thread indicating which port must be used to communicate. The problem is that if I start the thread and wait for a response, the client script will sit and wait for the server thread to complete, instead of moving on and communicating with it via sockets.

Is there a way for PHP, when run from CLI or shell_exec() to say, "Okay, that's the last of my output, go ahead and continue on."?

My structure is mostly as follows:

Server.php

Start socket listener
Output chosen port
Do { Application Loop }

Client.php

Start Session
If !Session['port'] {
    Start Server.php, receive Port //This is where it waits forever
}
Communicate with server thread via Port

I hope this is clear. Please comment if it is not.

  • 写回答

1条回答 默认 最新

  • douzhan4522 2015-08-25 16:33
    关注


    You can use PHP_THREAD :)
    Also, you can take a look to my Github project, it includes Socket and Thread examples and i think that it can help you : ANYEM_THREAD_TEST

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序