Home arrow PHP arrow Page 5 - Socket Programming With PHP

On Web-bed Feet - 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.

  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


Thus far, I've been using a standard telnet client to connect to my socket server and interact with it. However, it's just as easy to write a simple socket client in PHP. Consider the following example, which requests user input through an HTML form and creates a client connection to the server demonstrated a few pages back. The user's input is sent from the client to the server via this newly-minted socket connection, and the return value from the server (the same string, but reversed) is displayed to the user on an HTML page.

<html> <head> </head> <body> <? // form not yet submitted if (!$submit) { ?> <form action="<? echo $PHP_SELF; ?>" method="post"> Enter some text:<br> <input type="Text" name="message" size="15"><input type="submit" name="submit" value="Send"> </form> <? } else { // form submitted // where is the socket server? $host=""; $port = 1234; // open a client connection $fp = fsockopen ($host, $port, $errno, $errstr); if (!$fp) { $result = "Error: could not open socket connection"; } else { // get the welcome message fgets ($fp, 1024); // write the user string to the socket fputs ($fp, $message); // get the result $result .= fgets ($fp, 1024); // close the connection fputs ($fp, "END"); fclose ($fp); // trim the result and remove the starting ? $result = trim($result); $result = substr($result, 2); // now print it to the browser } ?> Server said: <b><? echo $result; ?></b> <? } ?> </body> </html>

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: