Miscellaneous Code
  Home arrow Miscellaneous Code arrow PHP TCP / UDP Network Client Class (w/...
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
MISCELLANEOUS CODE

PHP TCP / UDP Network Client Class (w/ Example)
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2004-01-20

    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


    The TcpClient class wrappers the networking details for reading/writing to a network stream into a simple, easy-to-use class (much like .NET TcpClient). An example use, connecting to a POP mail server, is attached. Change the POP server details and username/password at the top of the file to test.

    By : wmfwlr

    <!-- EXAMPLE SCRIPT USING CLASS -->
    <font face="verdana" size="2">
    <?
    include('class.TcpClient.php');

    //** Displays the error message given if there is non-empty text.

    function ShowError($errortext)
    {
    if(strlen($errortext) > 0)
    print("<font color='red'>$errortext</font><br>");
    }
    //** the POP server address, user account, and password to use for
    //** testing client application. NOTE: your POP user name is your
    //** full email address, unless the server is otherwise configured.

    $PopServer = "popserver.yourdomain.com";
    $PopUser = "email@yourdomain.com";
    $PopPassword = "yourpassword";

    //** connect to the POP server given on 110, the standard POP port.
    //** Change this if necessary.

    $client = new TcpClient($PopServer, 110);

    //** set the default read/write timeout to 2 seconds (2000 ms).

    $client->Timeout = 2000;

    //** you can set the default newline character(s) used when sending data.

    $client->NewLine = "\r\n";

    //** connect to server and get any error message (if available).

    $client->Connect();
    ShowError($client->GetError());

    //** get the mailserver greeting (one line).

    print("<b>" . $client->ReadLine() . "</b><br>");
    ShowError($client->GetError());

    //** send user login info to the server.

    print($client->WriteLine("USER $PopUser") . " bytes written<br>");
    ShowError($client->GetError());

    //** get the mailserver USER response message.

    print("<b>" . $client->ReadLine() . "</b><br>");
    ShowError($client->GetError());

    //** send user account password to the server.

    print($client->WriteLine("PASS $PopPassword") . " bytes written<br>");
    ShowError($client->GetError());

    //** get the mailserver PASS response message, login success

    print("<b>" . $client->ReadLine() . "</b><br>");
    ShowError($client->GetError());

    //**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //**
    //** Put any other commands for the POP server here!
    //** Full command list @ http://www.ietf.org/rfc/rfc1939.txt
    //**
    //**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    //** notify the mail server connection is closing.

    print($client->WriteLine("QUIT") . " bytes written<br>");
    ShowError($client->GetError());

    //** get the mailserver farewell message.

    print("<b>" . $client->ReadLine() . "</b><br>");
    ShowError($client->GetError());

    $client->Close();
    ?>
    </font>

    <?
    //-- CLASS FILE FOR TCP CLIENT --
    //** ©William Fowler (wmfwlr@cogeco.ca)
    //** DECEMBER 22/2003, Version 1.0

    if(isset($GLOBALS["tcpclientclass_php"])) { return; } //** include once.
    $GLOBALS["tcpclientclass_php"] = 1; //** file included.

    //** error code indicating that no connection is available.

    define("TcpClientNoConnection", 0);

    //** the default number of milliseconds to wait before timing out.

    define("TcpClientDefaultTimeout", 30000);

    //** the minimum buffer size for the TCP client class.

    define("TcpClientMinBufferSize", 256);

    //** the default newline character(s) used.

    define("TcpClientNewLine", "\r\n");

    //**!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //**TCP_CLIENT_CLASS_DEFINITION***********************************************
    //** The TcpClient class encapsulates most networking communication details
    //** into an easy-to-use wrapper. Connections are not persistant.

    class TcpClient
    {
    //** (Integer) the internal underlying socket identifier.

    var $Socket = TcpClientNoConnection;

    //** (String) the name/IP of the remote host

    var $Host;

    //** (Integer) the port number to connect to remote host.

    var $Port = 0;

    //** (String) the last error that occurred (NULL if no error).

    var $LastError = null;

    //** (String) the newline character to be used when sending data.

    var $NewLine = TcpClientNewLine;

    //** (Integer) the size of the buffer used to receive data.

    var $ReceiveBufferSize = TcpClientMinBufferSize;

    //** (Integer) the number of milliseconds to wait before aborting a connect,
    //** read, or write operation for the client.

    var $Timeout = TcpClientDefaultTimeout;

    //** creates a new TcpClient instance based on the hostname and port given.

    function TcpClient($remoteHost=null, $portNum=0)
    {
    $this->Host = strval($remoteHost);
    $this->Port = intval(max(0, $portNum)); //** ensure port >= than zero.
    }
    //** Returns: Boolean
    //** Determine whether or not a connection to the remote host has been
    //** established.

    function isOpen()
    {
    return ($this->Socket ? true : false);
    }
    //** Returns: String
    //** Get the last recorded error that occurred. If no error previously
    //** occurred NULL is returned. The last error is cleared.

    function GetError()
    {
    $theerror = $this->LastError;
    $this->ClearError();
    return $theerror;
    }
    //** Returns: None
    //** Clear the last error stored for this client.

    function ClearError()
    {
    $this->LastError = null;
    }
    //** Returns: Boolean
    //** Attemept to connect to the client remote host on the port number given.
    //** If no host is available FALSE is returned and no connection is made.
    //** If FALSE is returned the last error is available from the 'GetError()'
    //** method.

    function Connect()
    {
    $this->ClearError(); //** clear any existing error messages.

    //** no host name/IP has been set for this client, no connection can be made.
    //** Store the appropriate error.

    if(strlen(trim($this->Host)) == 0)
    {
    $this->LastError = "No remote host was provided";
    return false;
    }
    //** attempt to connect to the host on the port given. Record any error number
    //** and error message generated.

    $errorNum = 0;
    $this->Socket = fsockopen($this->Host, $this->Port, &$errorNum,
    &$this->LastError, ($this->Timeout / 1000));

    //** if there is no error given and the socket is valid connection is okay.

    return ($this->isOpen() && strlen(trim($this->LastError)) == 0);
    }
    //** Returns: Boolean
    //** Attempt to close the open client connection.

    function Close()
    {
    //** no open cobnnection is available. Set error appropriately.

    if(!$this->isOpen())
    $this->LastError = "No connection available to close";

    //** an open connection is available to close. Close underlying socket.

    else
    {
    fclose($this->Socket); //** clsoe the connection.
    $this->Socket = TcpClientNoConnection; //** no connection now.
    }
    return !$this->isOpen(); //** return the close operation success.
    }
    //** Returns: Integer
    //** Attempt to write the data given to the underlying socket. The number of
    //** bytes successfully written to the stream is returned. If no connection
    //** is available 0 is returned, as no bytes were written.

    function Write($data=null)
    {
    //** no connection is available, zero bytes can be sent.

    if(!$this->isOpen())
    {
    $this->LastError = "No connection available for writing";
    return 0;
    }
    $data = strval($data); //** ensure that data is available.
    if(strlen($data) == 0) //** no data to be sent.
    return 0; //** zero bytes were sent.
    else //** connection and data, set timeout and send.
    {
    $this->_SetTimeout(); //** set timeout.
    return fwrite($this->Socket, $data, strlen($data)); //** write data.
    }
    }
    //** Returns: Integer
    //** Attempt to write the data given to the underlying socket, followed by a
    //** newline. The newline is defined by the '$this->NewLine' property. The
    //** number of bytes actually written is returned.

    function WriteLine($data=null)
    {
    return $this->Write($data . $this->NewLine);
    }
    //** Returns: String
    //** Attempt to read the number of bytes given (or one by default) from the
    //** connection. If no connection is available, the length given is zero or
    //** negative, or an error occurs NULL is returned.

    function Read($length=1)
    {
    if(intval($length) <= 0)
    {
    $this->LastError = "Cannot read zero or less bytes";
    return null;
    }
    //** no connection is available to read from, no data can be read.

    else if(!$this->isOpen())
    {
    $this->LastError = "No connection available for reading";
    return null;
    }
    else //** a valid connection identifier is available.
    {
    $this->_SetTimeout(); //** ensure timeout is set.
    return fread($this->Socket, $length); //** attempt to read n-bytes.
    }
    }
    //** Returns: String
    //** Attempt to read one full line from the underlying stream. If no
    //** connection is available NULL is returned. Any newline characters
    //** are included in the string returned.

    function ReadLine()
    {
    //** no connection is available to read from, no data can be read.

    if(!$this->isOpen())
    {
    $this->LastError = "No connection available for reading";
    return null;
    }
    //** continue to read in data until a line ends with the newline character(s).
    //** This is safe as the 'fgets()' function will not read past a newline
    //** character. Ensure that the read buffer is at least the minumum size. If
    //** one iteration is complete and no data was read in the socket blocking
    //** expired. Stop reading at that point.

    $streamdata = ""; //** no data to start with.
    $sockethasexpired = false; //** initially no timeout experienced.

    while(!$this->_EndsWithNewLine($streamdata) && !$sockethasexpired)
    {
    $this->_SetTimeout(); //** ensure socket timeout is set.

    $streamdata .= fgets($this->Socket, max(TcpClientMinBufferSize,
    $this->ReceiveBufferSize));

    //** if ever at the point where reading has occurred and no data is available
    //** a socket timeout has occurred. Exit the loop and set the error.

    if(strlen($streamdata) == 0)
    {
    $sockethasexpired = true;
    $this->LastError = "The read took longer than $this->Timeout ms";
    }
    }
    return $streamdata; //** return the data received, including newline.
    }
    //** Returns: Boolean
    //** Determine whether or not the string given ends with a newline character.
    //** If no data is given FALSE is returned.

    function _EndsWithNewLine($data=null)
    {
    $data = strval($data); //** ensure that data is a string.
    if(strlen($data) == 0) //** no date given to test.
    return false; //** does not end with newline.

    //** get the position of the last newline character. If the value returned
    //** is not numeric it is a boolean, indicating no match. Considered to end
    //** with newline if it ends with either a '\n' or '\r' character.

    $creturnpos = strrpos($data, "\r"); //** position of '\n'.
    $newlinepos = strrpos($data, "\n"); //** position of '\r'.

    return (is_int($newlinepos) || is_int($creturnpos));
    }
    //** Returns: None
    //** Set the underlying socket timeout to the 'Timeout' value for this
    //** instance. If not connected nothing is done.

    function _SetTimeout()
    {
    //** if a connection is available set the socket timeout. Get the number of
    //** seconds and microseconds form the instance 'Timeout' property.

    if($this->isOpen())
    {
    stream_set_timeout($this->Socket, intval($this->Timeout / 1000),
    intval(($this->Timeout % 1000) * 1000));
    }
    }
    }
    ?>

    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 Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Addressing software-as-a-service challenges using Tivoli security and WebSphere solutions

    Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base.
    FREE! Go There Now!


    NEW! Cook up Web sites fast with CakePHP, Part 4: Use CakePHP&apos;s Session and Request Handler components

    CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP.
    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 Lotus Sametime Standard V8.0

    Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration.
    FREE! Go There Now!


    NEW! IBM Enterprise Modernization Sandbox for System z

    IBM Enterprise Modernization solutions help organizations evolve core IT systems towards modern architectures and technologies—reducing the burden of maintenance and freeing up resources to develop new business requirements and capabilities. With the IBM Enterprise Modernization Sandbox for System z you can evaluate IBM Enterprise Modernization solutions focused on five key areas: Assets, Architectures, Skills, Processes and Infrastructures, and Investment. Each solution is based upon real customer experiences and offers a proven path to get you started with your modernization projects.
    FREE! Go There Now!


    NEW! Maintaining QoS and Process Integrity in an SOA Environment

    This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”.
    FREE! Go There Now!


    NEW! Rational Talks to You:Per Kroll on Rational Method Composer Plug-in customization

    Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





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