I need to have my PHP application (its a stand alone, not web-based) connect to a server, and also host a tcp socket for things to connect to it.. but since php isnt multi-threaded, i cant have the server listen on one socket, and host another at the same time! it all has to be in one file. is it poss开发者_运维问答ible to run both of these side by side?
As an alternative to konforce's answer, use socket_select()
to listen to both sockets at once. It will tell you which sockets are able to be read/written when it returns. As pdb and konforce both rightly pointed out, you'll need to put the socket in non-blocking mode with socket_set_nonblock()
. Once socket_select()
tells you that a socket is ready, write or read as much as possible for each ready socket, then call socket_select()
again.
You will need both, socket_select AND non-blocking sockets.
For example socket_select tells you that the socket is writable but doesn't tell you how much bytes you can send without blocking.
Use non blocking sockets. See socket_set_nonblock()
and related functions.
精彩评论