Home arrow PHP arrow Page 4 - Socket Programming With PHP

Looping The Loop - PHP

You might not know this, but PHP comes with a very capable socketprogramming API. These socket functions now include almost everything youwould need for socket-based client-server communication over TCP/IP, andcan be easily deployed to build simple network applications. Find out more,inside.

TABLE OF CONTENTS:
  1. Socket Programming With PHP
  2. Putting It All Together
  3. Fortune's Fool
  4. Looping The Loop
  5. On Web-bed Feet
  6. Different Strokes
  7. POP Goes The Weasel
  8. Access Denied
  9. Game Over
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 204
February 05, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
You can also write a server that allows for more than just one transaction at a time. Consider the following variant of the first example:

<? // don't timeout set_time_limit (0); // set some variables $host = "192.168.1.99"; $port = 1234; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // bind socket to port $result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n"); // start listening for connections $result = socket_listen($socket, 3) or die("Could not set up socket listener\n"); echo "Waiting for connections...\n"; // accept incoming connections // spawn another socket to handle communication $spawn = socket_accept($socket) or die("Could not accept incoming connection\n"); echo "Received connection request\n"; // write a welcome message to the client $welcome = "Roll up, roll up, to the greatest show on earth!\n? "; socket_write($spawn, $welcome, strlen ($welcome)) or die("Could not send connect string\n"); // keep looping and looking for client input do { // read client input $input = socket_read($spawn, 1024, 1) or die("Could not read input\n"); 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 socket_close($spawn); break; } // otherwise... else { // reverse client input and send back $output = strrev($input) . "\n"; socket_write($spawn, $output . "? ", strlen (($output)+2)) or die("Could not write output\n"); echo "Sent output: " . trim($output) . "\n"; } } } while (true); // close primary socket socket_close($socket); echo "Socket terminated\n"; ?>
This is almost exactly the same code as that used in the very first example, with the obvious addition of debug messages and a "do-while" loop which allows the server to keep reading new data transmitted by the client, and responding to it. The loop is terminated when the client sends the special session terminator string "END".

Here's the output of a sample session:

$ telnet 192.168.1.99 1234 Trying 192.168.1.99... Connected to medusa. Escape character is '^]'. Roll up, roll up, to the greatest show on earth! ? Look Ma...all backwards sdrawkcab lla...aM kooL ? jack frost tsorf kcaj ? END Connection closed by foreign host.
And here are the corresponding debug messages generated on the server:

$ /usr/local/bin/php -q server.php Waiting for connections... Received connection request Received input: Look Ma...all backwards Sent output: sdrawkcab lla...aM kooL Received input: jack frost Sent output: tsorf kcaj Received input: END Socket terminated


 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: