PHP
  Home arrow PHP arrow Page 6 - Socket Programming With PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Socket Programming With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 159
    2002-02-05


    Table of Contents:
  • Socket Programming With PHP
  • Putting It All Together
  • Fortune's Fool
  • Looping The Loop
  • On Web-bed Feet
  • Different Strokes
  • POP Goes The Weasel
  • Access Denied
  • Game Over

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Socket Programming With PHP - Different Strokes
    ( Page 6 of 9 )

    If you'd prefer to, there's also an alternative, somewhat longer approach to constructing a client. Most of the time, you won't need to use this - fsockopen() is more than sufficient for most requirements - but it's included here for reference purposes. Take a look at this next script, which replicates the functionality of the previous example:

    <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="192.168.1.99"; $port = 1234; // create socket $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n"); // connect to server $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n"); socket_read ($socket, 1024) or die("Could not read server response\n"); // send string to server socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n"); // get server response $result = socket_read ($socket, 1024) or die("Could not read server response\n"); // end session socket_write($socket, "END", 3) or die("Could not end session\n"); // close socket socket_close($socket); // clean up result $result = trim($result); $result = substr($result, 0, strlen($result)-1); // print result to browser ?> Server said: <b><? echo $result; ?></b> <? } ?> </body> </html>
    In this case, the socket_connect() function is used to open a connection to the server, with the familiar socket_read() and socket_write() functions used to receive and transmit data over the socket connection. Once the result string has been obtained from the server, the socket connection is closed with socket_close() and the output is printed to the browser.

    Again, this is an alternative implementation - it's unlikely that you'll find much use for it, as the fsockopen() function provides a much simpler (and shorter) way to accomplish the same thing.

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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT