Net_Socket
[ class tree: Net_Socket ] [ index: Net_Socket ] [ all elements ]

Source for file Socket.php

Documentation is available at Socket.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Chuck Hagenbuch <chuck@horde.org>                           |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Socket.php,v 1.22 2005/01/11 22:02:23 chagenbu Exp $
  21.  
  22. require_once 'PEAR.php';
  23.  
  24. define('NET_SOCKET_READ',  1);
  25. define('NET_SOCKET_WRITE'2);
  26. define('NET_SOCKET_ERROR'3);
  27.  
  28. /**
  29.  * Generalized Socket class.
  30.  *
  31.  * @version 1.1
  32.  * @author Stig Bakken <ssb@php.net>
  33.  * @author Chuck Hagenbuch <chuck@horde.org>
  34.  */
  35. class Net_Socket extends PEAR {
  36.  
  37.     /**
  38.      * Socket file pointer.
  39.      * @var resource $fp 
  40.      */
  41.     var $fp = null;
  42.  
  43.     /**
  44.      * Whether the socket is blocking. Defaults to true.
  45.      * @var boolean $blocking 
  46.      */
  47.     var $blocking = true;
  48.  
  49.     /**
  50.      * Whether the socket is persistent. Defaults to false.
  51.      * @var boolean $persistent 
  52.      */
  53.     var $persistent = false;
  54.  
  55.     /**
  56.      * The IP address to connect to.
  57.      * @var string $addr 
  58.      */
  59.     var $addr '';
  60.  
  61.     /**
  62.      * The port number to connect to.
  63.      * @var integer $port 
  64.      */
  65.     var $port = 0;
  66.  
  67.     /**
  68.      * Number of seconds to wait on socket connections before assuming
  69.      * there's no more data. Defaults to no timeout.
  70.      * @var integer $timeout 
  71.      */
  72.     var $timeout = false;
  73.  
  74.     /**
  75.      * Number of bytes to read at a time in readLine() and
  76.      * readAll(). Defaults to 2048.
  77.      * @var integer $lineLength 
  78.      */
  79.     var $lineLength = 2048;
  80.  
  81.     /**
  82.      * Connect to the specified port. If called when the socket is
  83.      * already connected, it disconnects and connects again.
  84.      *
  85.      * @param string  $addr  IP address or host name.
  86.      * @param integer $port  TCP port number.
  87.      * @param boolean $persistent  (optional) Whether the connection is
  88.      *                              persistent (kept open between requests
  89.      *                              by the web server).
  90.      * @param integer $timeout  (optional) How long to wait for data.
  91.      * @param array $options  See options for stream_context_create.
  92.      * @access public
  93.      * @return mixed true on success or error object
  94.      */
  95.     function connect($addr$port = 0$persistent = null$timeout = null$options = null)
  96.     {
  97.         if (is_resource($this->fp)) {
  98.             @fclose($this->fp);
  99.             $this->fp = null;
  100.         }
  101.  
  102.         if (!$addr{
  103.             return $this->raiseError('$addr cannot be empty');
  104.         elseif (strspn($addr'.0123456789'== strlen($addr)) {
  105.             $this->addr $addr;
  106.         else {
  107.             $this->addr @gethostbyname($addr);
  108.         }
  109.  
  110.         $this->port $port % 65536;
  111.  
  112.         if ($persistent !== null{
  113.             $this->persistent $persistent;
  114.         }
  115.  
  116.         if ($timeout !== null{
  117.             $this->timeout $timeout;
  118.         }
  119.  
  120.         $openfunc $this->persistent 'pfsockopen' 'fsockopen';
  121.         $errno = 0;
  122.         $errstr '';
  123.         if ($options && function_exists('stream_context_create')) {
  124.             if ($this->timeout{
  125.                 $timeout $this->timeout;
  126.             else {
  127.                 $timeout = 0;
  128.             }
  129.             $context stream_context_create($options);
  130.             $fp @$openfunc($this->addr$this->port$errno$errstr$timeout$context);
  131.         else {
  132.             if ($this->timeout{
  133.                 $fp @$openfunc($this->addr$this->port$errno$errstr$this->timeout);
  134.             else {
  135.                 $fp @$openfunc($this->addr$this->port$errno$errstr);
  136.             }
  137.         }
  138.  
  139.         if (!$fp{
  140.             return $this->raiseError($errstr$errno);
  141.         }
  142.  
  143.         $this->fp $fp;
  144.  
  145.         return $this->setBlocking($this->blocking);
  146.     }
  147.  
  148.     /**
  149.      * Disconnects from the peer, closes the socket.
  150.      *
  151.      * @access public
  152.      * @return mixed true on success or an error object otherwise
  153.      */
  154.     function disconnect()
  155.     {
  156.         if (is_resource($this->fp)) {
  157.             @fclose($this->fp);
  158.             $this->fp = null;
  159.             return true;
  160.         }
  161.         return $this->raiseError('not connected');
  162.     }
  163.  
  164.     /**
  165.      * Find out if the socket is in blocking mode.
  166.      *
  167.      * @access public
  168.      * @return boolean  The current blocking mode.
  169.      */
  170.     function isBlocking()
  171.     {
  172.         return $this->blocking;
  173.     }
  174.  
  175.     /**
  176.      * Sets whether the socket connection should be blocking or
  177.      * not. A read call to a non-blocking socket will return immediately
  178.      * if there is no data available, whereas it will block until there
  179.      * is data for blocking sockets.
  180.      *
  181.      * @param boolean $mode  True for blocking sockets, false for nonblocking.
  182.      * @access public
  183.      * @return mixed true on success or an error object otherwise
  184.      */
  185.     function setBlocking($mode)
  186.     {
  187.         if (is_resource($this->fp)) {
  188.             $this->blocking $mode;
  189.             socket_set_blocking($this->fp$this->blocking);
  190.             return true;
  191.         }
  192.         return $this->raiseError('not connected');
  193.     }
  194.  
  195.     /**
  196.      * Sets the timeout value on socket descriptor,
  197.      * expressed in the sum of seconds and microseconds
  198.      *
  199.      * @param integer $seconds  Seconds.
  200.      * @param integer $microseconds  Microseconds.
  201.      * @access public
  202.      * @return mixed true on success or an error object otherwise
  203.      */
  204.     function setTimeout($seconds$microseconds)
  205.     {
  206.         if (is_resource($this->fp)) {
  207.             socket_set_timeout($this->fp$seconds$microseconds);
  208.             return true;
  209.         }
  210.         return $this->raiseError('not connected');
  211.     }
  212.  
  213.     /**
  214.      * Returns information about an existing socket resource.
  215.      * Currently returns four entries in the result array:
  216.      *
  217.      * <p>
  218.      * timed_out (bool) - The socket timed out waiting for data<br>
  219.      * blocked (bool) - The socket was blocked<br>
  220.      * eof (bool) - Indicates EOF event<br>
  221.      * unread_bytes (int) - Number of bytes left in the socket buffer<br>
  222.      * </p>
  223.      *
  224.      * @access public
  225.      * @return mixed Array containing information about existing socket resource or an error object otherwise
  226.      */
  227.     function getStatus()
  228.     {
  229.         if (is_resource($this->fp)) {
  230.             return socket_get_status($this->fp);
  231.         }
  232.         return $this->raiseError('not connected');
  233.     }
  234.  
  235.     /**
  236.      * Get a specified line of data
  237.      *
  238.      * @access public
  239.      * @return $size bytes of data from the socket, or a PEAR_Error if
  240.      *          not connected.
  241.      */
  242.     function gets($size)
  243.     {
  244.         if (is_resource($this->fp)) {
  245.             return @fgets($this->fp$size);
  246.         }
  247.         return $this->raiseError('not connected');
  248.     }
  249.  
  250.     /**
  251.      * Read a specified amount of data. This is guaranteed to return,
  252.      * and has the added benefit of getting everything in one fread()
  253.      * chunk; if you know the size of the data you're getting
  254.      * beforehand, this is definitely the way to go.
  255.      *
  256.      * @param integer $size  The number of bytes to read from the socket.
  257.      * @access public
  258.      * @return $size bytes of data from the socket, or a PEAR_Error if
  259.      *          not connected.
  260.      */
  261.     function read($size)
  262.     {
  263.         if (is_resource($this->fp)) {
  264.             return @fread($this->fp$size);
  265.         }
  266.         return $this->raiseError('not connected');
  267.     }
  268.  
  269.     /**
  270.      * Write a specified amount of data.
  271.      *
  272.      * @access public
  273.      * @return mixed true on success or an error object otherwise
  274.      */
  275.     function write($data)
  276.     {
  277.         if (is_resource($this->fp)) {
  278.             return fwrite($this->fp$data);
  279.         }
  280.         return $this->raiseError('not connected');
  281.     }
  282.  
  283.     /**
  284.      * Write a line of data to the socket, followed by a trailing "\r\n".
  285.      *
  286.      * @access public
  287.      * @return mixed fputs result, or an error
  288.      */
  289.     function writeLine ($data)
  290.     {
  291.         if (is_resource($this->fp)) {
  292.             return fwrite($this->fp$data "\r\n");
  293.         }
  294.         return $this->raiseError('not connected');
  295.     }
  296.  
  297.     /**
  298.      * Tests for end-of-file on a socket descriptor.
  299.      *
  300.      * @access public
  301.      * @return bool 
  302.      */
  303.     function eof()
  304.     {
  305.         return (is_resource($this->fp&& feof($this->fp));
  306.     }
  307.  
  308.     /**
  309.      * Reads a byte of data
  310.      *
  311.      * @access public
  312.      * @return byte of data from the socket, or a PEAR_Error if
  313.      *          not connected.
  314.      */
  315.     function readByte()
  316.     {
  317.         if (is_resource($this->fp)) {
  318.             return ord(@fread($this->fp1));
  319.         }
  320.         return $this->raiseError('not connected');
  321.     }
  322.  
  323.     /**
  324.      * Reads a word of data
  325.      *
  326.      * @access public
  327.      * @return word of data from the socket, or a PEAR_Error if
  328.      *          not connected.
  329.      */
  330.     function readWord()
  331.     {
  332.         if (is_resource($this->fp)) {
  333.             $buf @fread($this->fp2);
  334.             return (ord($buf[0](ord($buf[1]<< 8));
  335.         }
  336.         return $this->raiseError('not connected');
  337.     }
  338.  
  339.     /**
  340.      * Reads an int of data
  341.      *
  342.      * @access public
  343.      * @return integer  1 int of data from the socket, or a PEAR_Error if
  344.      *                   not connected.
  345.      */
  346.     function readInt()
  347.     {
  348.         if (is_resource($this->fp)) {
  349.             $buf @fread($this->fp4);
  350.             return (ord($buf[0](ord($buf[1]<< 8+
  351.                     (ord($buf[2]<< 16(ord($buf[3]<< 24));
  352.         }
  353.         return $this->raiseError('not connected');
  354.     }
  355.  
  356.     /**
  357.      * Reads a zeroterminated string of data
  358.      *
  359.      * @access public
  360.      * @return string, or a PEAR_Error if
  361.      *          not connected.
  362.      */
  363.     function readString()
  364.     {
  365.         if (is_resource($this->fp)) {
  366.             $string '';
  367.             while (($char @fread($this->fp1)) != "\x00")  {
  368.                 $string .= $char;
  369.             }
  370.             return $string;
  371.         }
  372.         return $this->raiseError('not connected');
  373.     }
  374.  
  375.     /**
  376.      * Reads an IP Address and returns it in a dot formated string
  377.      *
  378.      * @access public
  379.      * @return Dot formated string, or a PEAR_Error if
  380.      *          not connected.
  381.      */
  382.     function readIPAddress()
  383.     {
  384.         if (is_resource($this->fp)) {
  385.             $buf @fread($this->fp4);
  386.             return sprintf("%s.%s.%s.%s"ord($buf[0])ord($buf[1]),
  387.                            ord($buf[2])ord($buf[3]));
  388.         }
  389.         return $this->raiseError('not connected');
  390.     }
  391.  
  392.     /**
  393.      * Read until either the end of the socket or a newline, whichever
  394.      * comes first. Strips the trailing newline from the returned data.
  395.      *
  396.      * @access public
  397.      * @return All available data up to a newline, without that
  398.      *          newline, or until the end of the socket, or a PEAR_Error if
  399.      *          not connected.
  400.      */
  401.     function readLine()
  402.     {
  403.         if (is_resource($this->fp)) {
  404.             $line '';
  405.             $timeout time($this->timeout;
  406.             while (!feof($this->fp&& (!$this->timeout || time($timeout)) {
  407.                 $line .= @fgets($this->fp$this->lineLength);
  408.                 if (substr($line-2== "\r\n" ||
  409.                     substr($line-1== "\n"{
  410.                     return rtrim($line"\r\n");
  411.                 }
  412.             }
  413.             return $line;
  414.         }
  415.         return $this->raiseError('not connected');
  416.     }
  417.  
  418.     /**
  419.      * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
  420.      * socket is in blocking mode until the socket closes.
  421.      *
  422.      * @access public
  423.      * @return All data until the socket closes, or a PEAR_Error if
  424.      *          not connected.
  425.      */
  426.     function readAll()
  427.     {
  428.         if (is_resource($this->fp)) {
  429.             $data '';
  430.             while (!feof($this->fp)) {
  431.                 $data .= @fread($this->fp$this->lineLength);
  432.             }
  433.             return $data;
  434.         }
  435.         return $this->raiseError('not connected');
  436.     }
  437.  
  438.     /**
  439.      * Runs the equivalent of the select() system call on the socket
  440.      * with a timeout specified by tv_sec and tv_usec.
  441.      *
  442.      * @param integer $state    Which of read/write/error to check for.
  443.      * @param integer $tv_sec   Number of seconds for timeout.
  444.      * @param integer $tv_usec  Number of microseconds for timeout.
  445.      *
  446.      * @access public
  447.      * @return False if select fails, integer describing which of read/write/error
  448.      *          are ready, or PEAR_Error if not connected.
  449.      */
  450.     function select($state$tv_sec$tv_usec = 0)
  451.     {
  452.         if (is_resource($this->fp)) {
  453.             $read   = null;
  454.             $write  = null;
  455.             $except = null;
  456.             if ($state NET_SOCKET_READ{
  457.                 $read[$this->fp;
  458.             }
  459.             if ($state NET_SOCKET_WRITE{
  460.                 $write[$this->fp;
  461.             }
  462.             if ($state NET_SOCKET_ERROR{
  463.                 $except[$this->fp;
  464.             }
  465.             if (false === ($sr stream_select($read$write$except$tv_sec$tv_usec))) {
  466.                 return false;
  467.             }
  468.  
  469.             $result = 0;
  470.             if (count($read)) {
  471.                 $result |= NET_SOCKET_READ;
  472.             }
  473.             if (count($write)) {
  474.                 $result |= NET_SOCKET_WRITE;
  475.             }
  476.             if (count($except)) {
  477.                 $result |= NET_SOCKET_ERROR;
  478.             }
  479.             return $result;
  480.         }
  481.  
  482.         return $this->raiseError('not connected');
  483.     }
  484.  
  485. }

Documentation generated on Mon, 11 Mar 2019 14:12:52 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.