Do you have a question? Post it now! No Registration Necessary.  Now with pictures!


I'm trying to familiarise myself with socket programming in PHP on a XP Home
SP2 box with Apache 2.0.58 / PHP 5.1.4.

I've ran a few basic scripts now which I accessed through a telnet
session. The purpose of these scripts is to simply return the input I type.
They all pretty much have the same routine as shown below:

-- begin snippet --

do {
  // read client input
  $input = socket_read($spawn, 1024, PHP_NORMAL_READ) or die("Could not read  

  if (trim($input) != "") {
    echo "Received input: $input\n";

    // if client requests session end
    if (trim($input) == "END") {
      // close the child socket
      // break out of loop
    // otherwise...
    else {
      // reverse client input and send back
      $output = strrev($input) . "\n";
      socket_write($spawn, $output . "? ", strlen (($output)+3)) or
die("Could not write output\n");
      echo "Sent output: " . trim($output) . "\n";
} while (true);

-- end code snippet --  

Now, I read that the PHP_NORMAL_READ argument to socket_read() is buggy.  
This seems to be true for my configuration as well. When ommitting this  
argument the server seems to run fine except for 1 thing: The server returns  
every single character I type immediately, thus refusing to accept the 'END'  
request for instance. I would like to let the server return my input only  
when I have hit enter. The PHP manual seems to tell me that that is just  
exactly what the PHP_NORMAL_READ argument is supposed to accomplish.

Is this true? And if so, is there a workaround for this?

My goal is to write a low-end game server communicating with a flash client.  
I could imagine that in that case this isn't so much of an issue anymore  
since this client will probably send a whole 'command' string at a time. Is  
this a correct assumption?

Thanks for your input.  

Site Timeline