PHP Sockets - Accept multiple connections

前端 未结 4 1430
刺人心
刺人心 2021-02-05 13:12

I\'m trying to create a simple client/server application and thus I am experimenting with sockets in PHP.

Now I have a simple client in C# which connects to the server we

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 14:03

    Typically socket servers need to be multi-threaded if you want to handle > 1 client. You'd create a 'listen' thread and spawn a new 'answer' thread for each client request. Im not sure how PHP would handle a situation like this though. Perhaps it has a fork mechanism?

    EDIT: Doesn't appear that PHP offers threading per se (http://stackoverflow.com/questions/70855/how-can-one-use-multi-threading-in-php-applications) If you want to follow the typical paradigm for a socket server you might get away with using 'popen' to spawn a process to handle the child request. Hand off the socket id and let it close itself when the child socket closes. You'd need to keep on top of this list to avoid orphaning these processes if your server process closes.

    FWIW: here are some examples of multi-client servers: http://php.net/manual/en/function.socket-accept.php

提交回复
热议问题