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!


    NEW! Build Web services with transport-level security using Rational Application Developer V7, Part 1: Build Web services and Web services clients

    Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services.
    FREE! Go There Now!


    NEW! Discovering the value of WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    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 IBM Rational Developer for System i V7.1

    Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications.
    FREE! Go There Now!


    NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

    Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! IBM Rational Systems Development e-Kit

    As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br />
    FREE! Go There Now!


    NEW! Rational Asset Manager eKit

    Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions.
    FREE! Go There Now!


    NEW! Rational Talks to You: Grady Booch on Architecture

    Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered!
    FREE! Go There Now!


    NEW! Webcast: Striking the right balance between manual and automated testing

    Join this webcast to learn how IBM Rational's Functional Testing solution enables you to implement automation your way, at your pace, with your existing staff. In this webcast, you’ll learn how you can eliminate redundancy of manual test scripts, reduce errors, and increase test coverage through test automation. After this presentation you will understand how IBM Rational Functional Testing solution can streamline your manual testing and make test automation easily attainable.
    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
    For more Enterprise Application Development news, visit eWeek