Socket Programming With PHP - Putting It All Together (
Page 2 of 9 )
Now, how about seeing it in
action? Since this script generates an "always-on" socket, it isn't a good idea
to run it via your Web server; instead, you might prefer to run it from the
command line via the PHP binary:
$ /usr/local/bin/php -q server.php
In case you don't have a PHP binary, it's fairly easy to
compile one - just follow the installation instructions for compiling a static
Apache module (these instructions are available in the PHP distribution), but
omit the "--with-apache" parameter to the "configure" script.
Note the
additional -q parameter to PHP - this tells the program to suppress the
"Content-Type: text/html" header that it usually adds when executing a script (I
don't need this header here because the output of this script isn't going to a
browser).
Once the script has been executed and the socket server is
active, you can simply telnet to it using any standard telnet application, and
send it a string of characters as input. The server should respond with the
reversed string, and then terminate the connection. Here's what it looks like:
$ telnet 192.168.1.99 1234
Trying 192.168.1.99...
Connected to medusa.
Escape character is '^]'.
jack and the beanstalk
klatsnaeb eht dna kcaj
Connection closed by foreign host.