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 possible to run both of these side by side?
Share
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 withsocket_set_nonblock(). Oncesocket_select()tells you that a socket is ready, write or read as much as possible for each ready socket, then callsocket_select()again.