I've got a client who wants me to write a browser based program that will connect to a socket server given an ip address and port number (that the user would supply). Once the socket is connected, the user will type in a command into a text box and send it to the php server which would then relay the command via the socket to the waiting server on the already open connection. I would then need to read the servers response and relay that message to the user on-screen.
So, in my preliminary dabbling yesterday, I figured out how to open and close a connection to the socket server. I could even write to the socket and read the servers response. The problem is that each of these little tests were self contained. I had one php script that contained the connection code, the code to write a hard-coded command to the socket and then the code to read from the socket followed by the code to close the socket connection.
I started running into problems when I tried to make things more interactive. What I seemed to find was that I could not persist the socket handle across page requests. I started out by writing a little screen with two text boxes, a button and a div which would contain the responses from the socket. I wrote an AJAX function in javascript (jQuery 1.2.6) that called a php script sending the ip and port. That script created a socket to that server on that port and sent the automatic response back to the calling javascript function.
This worked great.
What I soon realized though, was that as soon as my opensocket.php script was finished running, was that I had no way of referring to the socket handle on subsequent requests. I tried storing the socket handle in $_SESSION['socket'], but that failed miserably, and from what I can find, it fails by design. For some reason (security, maybe?) php resource objects cannot be stored in the session. Drat!
It would seem like it wouldn't be a problem to open a connection to the socket server on each call, send the command, wait for the response from the server and then close the connection, right? But in this case, it's actually not okay. There are certain commands which when sent to the server return ids that are good for that connection only. Once the socket is closed, that id goes away.
One solution (which my client doesn't really like much) is instead of sending one command at a time, to send a bunch of commands. One reason he doesn't like this is that command 'a' might return an id or code which is needed by command 'b' and there would be know way to know that command a head of time.
So it would seem that I need a way to keep a socket open across several page requests.
Is that possible using PHP? Is there a strategy for doing this that I'm just not getting? I've done a bunch of reading about sockets on the internet, but everyone seems to be talking about turning a php server into a socket server which listens for other programs to connect to it. What *I* want is to connect to a different socket server that is already listening for my connection and waiting for me to issue commands over the open socket connection.
I realize this is kind of a long winded post, but I hope I've made some sense here, and that someone can help me to understand how I might be able to accomplish this task, if it's possible at all. It was a toss up as to which forum I should post it in, so if someone thinks that this discussion would be better put in the PHP Theory and Design forum, rather than this one, then let me know and I can cross post it over there.
Thanks,
Chris