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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器