PHP
  Home arrow PHP arrow Sockets and PHP
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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

Sockets and PHP
By: Demitrious Kelly
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2003-03-25

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    If you are an intermediate PHP developer you may have wanted to use sockets in connect to your FTP server. Demitrious shows you how this is done.

    Sockets have long been important to the underlying nature and structure of things in, on and around computers. They are very powerful. PHP allows you to harness them at the expense of a couple of late nights, and many cans of liquid caffeine yet, it seems, that too few people truly understand sockets.

    I suppose that before you can really use any functionality of sockets it's important to define what sockets are. A beginner should read this for sure, but learned folk (yea I just called all you l337 hax0rs out there 'folk', get over yourselves!), would only need this to refresh their memories as to what sockets are and what they mean.

    What are sockets?

    A socket is a lot like a pipe or a hose. It is meant to have matter travel through it. This "pipe" does not care what kind of matter will go through it, going through it or already went though it. Nor does it care where the matter is coming from or where it is going to. It's only encompassing goal is to let that matter through it. You can consider a socket (on a computer) to be the pipe which carries the data through it.

    Lets take a pathetic example of browsing the web - a task that most of us take for granted. Think about it:

    1) You open your web browser, and say "yonder to www.apokalyptik.com !"

    2) Your web browser responds by asking the operating system to make a socket connection to " www.apokalyptik.com ."

    3) From there, to be short, your OS (Operating System) uses the socket that it just created to the remote server's socket that is 'listening' on port 80.

    NOTE: I simplified this considerably. If I would write all the steps in details then it would be about 17 pages long.

    4) When the connection has been successfully established the 'data' (web page) can then flow through the pipes (sockets) and we can see it.

    Is this the say-all and end-all of sockets? Of course not!

    Log into any Unix box and type netstat -an - you will see a list of TCP sockets, UDP sockets, Active UNIX domain sockets (both, servers and established) and staff like that..

    There are two reasons I can see for using a Unix socket.

    1) You find that the PHP built-in functions do not suite your needs.

    2) You want to make your own service with its own protocols

    In any of these cases PHP accommodates you by allowing you to manipulate data at the service level, and bypass all of the middle men.

    How exactly does one go about exploiting sockets for fun and profit? Start with a careful planning and lots of research! Is it a bit of work, true, but are the rewards worth it!

    Case 1: You are interested in connecting to a service which PHP does not fully support, or you are not fully happy with its support.

    Step 1: Choose a service

    For this example I will choose a common protocol: FTP. It's not the easiest of the protocols to grasp, but afterwards, you should have a much keener grasp on how all of this works.

    Step 2: RTFM RTFM RTFM RTFM

    You cannot just expect things to magically work for you - you need to put some serious thoughts into how the service works, what you want from it and how you plan to make it working.

    Go to www.google.com and search for your service and the word (acronym) RFC (which stands for 'Request For Comment'). When we do this we get good info like: http://www.ietf.org/rfc/rfc959.txt .

    Reading this we find all the commands and rules that an FTP server and an FTP client must obey... Here's a brief and important summary. You have to use 2 sockets for most of your communications with the server. You have a control socket (commonly port 21) and a data socket (commonly port 22).

    To make things even more difficult for you (relax, I know what I'm doing) I proclaim that we will use PASSIVE mode FTP (see: http://slacksite.com/other/ftp.html for further details on what, exactly, passive mode is...)

    Step 3: Think Things Through!

    Well now we've got a lot to deal with variables, commands and the like... Let's try to keep things clean. We'll make a class for our FTP client. (see: Object Oriented John (Intro to Objects), one of my previous articles on PHP Beginner). All we want to do is log in, get a directory listing and log back out.

    To do this we need to be using the following variables:

    1. host name
    2. port number (default 21)
    3. username (default anonymous)
    4. password (default
    e-mail@email.com )

    To do this I figure we need the following functions

    1. control_socket()
    2. data_socket()
    3. login()
    4. passive()
    5. ftp_list()
    6. logout()
    7. cmd_status()

    We also need to keep track of the following:

    1. whats going out and coming in on the control socket

    2. whats coming in on the data socket (we never write anything to it)

    Step 4: Putting Things Together

    We'll start by defining our FTP class

    <?php
        class ftp {
            // Here is the skeletone of our ftp class. A shining beacon
            // of light in an otherwise desolate landscape of socket code....
            // The hostname of the computer we are going to connect to
            var $hostname;
                // The port number we will connect to on the host.
                // Not always port 21
            var $portnumb;
                // The username and password we will use to log into
                // the FTP site with (In case we're accessing a private server)
            var $password;    
            var $username;
                // The variable containing the control socket pointer
            var $contsock;
                // The variable containing the data socket pointer
            var $datasock;    
            function control_socket_reset() {
                // This function will reset the socket variables so that we
                // can use it again (in essence, we're providing our control
                // socket with a strong cup of coffee to keep it awake)
            }
            function control_socket($host, $port) {
                // This function will establish our 'control' connection
                // to the ftp server. This is the connection that we will
                // give all of our commands to...
            }
            function data_socket($host, $port) {
                // This function will establish our 'data' port which
                // will return any data that our commands provide.
                // in this example we will only enable this AFTER we've
                // passive()'d
            }
            function login($username='anonymous',
    $password='e-mail@e-mail.com' ) {
                // This will log us onto the ftp server. it will use either
                // the default anonymous login information, or info that you
                // call the function with
            }
            function passive() {
                // This function will get everything ready for us to set up our
                // data socket, and then actually initiate it by calling
                // data_socket($host, $port);
            }
            function ftp_list() {
                // This will get the directory listing that we want. This one
                // little function is causing us to do all this work. Such is
                // life. Often it's the little stupid things which you're boss
                // will have you work on for hours on end, while more pressing
                // issues fall by the wayside (it's, of course, your fault when
                // that happens ;)
            }
            function cmd_status() {
                // This code will find out whether a command was completed
                // successfully or not (very useful!)
            }
            function logout() {
                // Since we want to be good little net'zines we'll properly log
                // ourselves out of the server when we are finished... (what...
                // were you born in a barn?!)
            }
        }
    ?>

    PHEW! Was that a lot to grasp? that's ok... take some time... grab some aspirin, and a snack... Let the information soak in for a while, before moving on... You need to approach this topic with a clear mind, and a willingness to go the distance! (I SAID it would be some work...)

    Step 5: building our first connection

    Every journey begins with the length of a single step. It's now time to build our control socket. Don't worry, it's easier then it sounds...

    <?php
        function control_socket($host, $port) {
                // Store variables here just in case. We need them
                // at some later time (I know that we will not in this article,
                // but good record keeping has saved MANY a good programmer
            $this->hostname = $host;
            $this->portnumb = $port;
            $this->contsock = fsockopen($this->hostname, $this->portnumb);
                // establish the connection and keep it to find later again
            
    if ( $this->contsock ) {
                Return True;    // Return TRUE if our connection was successfull
            }
            else {
                Return False;    // Return FALSE if we failed!
            }
        }
    ?>

    Wasn't that simple? Realistically, it was a single function with some code thrown in to make sure that commection happened. It is always a good idea to check whether things go wrong, so we can close up any loose ends.

    ACK! Now we need to test! I generally believe that it's a very good idea to test your code at every point. It may be a little bit more time-consuming, but in the end, you end up with a more reliable, bullet-proof code and less bugs. We will throw this down to the bottom of our code.

    <?php
        $ftp = new ftp;
         if ( $ftp->control_socket('ftp.cdrom.com', 21) ) {
            echo 'We have successfully connected!<br>';
        }
        else {
            die('We have failed to connect!<br>');
        }
    ?>

    After testing it carefully we see that out control connection code works! Congratulations! But don't get carried away with yourself just yet - there's more difficulties to come then you, as of yet, understand!

    Step 6: Our daily dose of caffeine

    I'm going to let you in on a little secret which no one seems to know (or no one seems to want to share). Normally, when you fopen() a file, fgets() will return either something or an error, but when you open a socket with fsockopen(), fgets() will wait..., wait..., wait..., wait... and wait for something to be in the buffer. This SERIOUSLY slows application down. There is nothing, nothing with the fsockopen() docs in relation to this problem - nor even a mention of it. You need to use the socket_set_timeout() function that allows you to set a timeout with milliseconds resolution for fgets() on a socket. Tuck that info away in your PHP toolbox, you might need it some day!

    Want to know another secret? After you've timed out a socket (using fgets() and socket_set_timeout()) it stays timed out until you reset it again with socket_set_timeout(). No one seemed to know the answer to this little problem either, so store it away, because the likelyhood of you finding the answer when you most need it is very low :)

    With this in mind, we're going to setup our 'caffeine' function, control_socket_reset(), which will set our sockets timeout to a manageable 3 seconds.

    <?php
        function control_socket_reset() {
            if ( $this->contsock ) {
                    // Reset (or set) the timeout on our control socket
                return True;
            } else {
                return False;
            }
        }
    ?>

    With all this, you should be alble to connect to your FTP server. In the next week's article I will desribe how to login on your server, and pulling the data in and out of the FTP server using sockets!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More PHP Articles
    More By Demitrious Kelly

     

    IBM® developerWorks developerWorks - FREE Tools!


    Be the first to hear about i5/OS V6R1!

    Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br />
    FREE! Go There Now!


    IBM DB2 Deep Compression ROI Tool

    The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times.
    FREE! Go There Now!


    NEW! Download a free trial of Lotus Quickr 8.0

    Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications.
    FREE! Go There Now!


    NEW! Evaluate Rational Host Access Transformation Services (HATS) Toolkit V7.1

    Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications.
    FREE! Go There Now!


    NEW! Hello World: Learn how to install and use the Rational Asset Manager Eclipse client

    In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset.
    FREE! Go There Now!


    NEW! Hello World: WebSphere Service Registry and Repository

    Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z: Architecture

    Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server.
    FREE! Go There Now!


    NEW! Krugle, developerWorks, and code search

    Ken Krugler, co-founder of code search company Krugle, and Laura Merling, vice president of Marketing and Business Development for Krugle, join to talk about the ins and outs of code search and what it means as a new feature for developerWorks users.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Connectivity

    Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic.
    FREE! Go There Now!


    NEW! Webcast: IBM Rational Build Forge - Beyond the Build

    The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP







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