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.21 2004/12/13 21:45:59 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|| substr($addr01== '/'{
  105.             $this->addr $addr;
  106.         else {
  107.             $this->addr gethostbyname($addr);
  108.             if (strcmp($this->addr$addr== 0{
  109.                 return $this->raiseError("unable to lookup hostname '$addr'");
  110.             }
  111.         }
  112.         $this->port $port % 65536;
  113.         if ($persistent !== null{
  114.             $this->persistent $persistent;
  115.         }
  116.         if ($timeout !== null{
  117.             $this->timeout $timeout;
  118.         }
  119.         $openfunc $this->persistent 'pfsockopen' 'fsockopen';
  120.         $errno = 0;
  121.         $errstr '';
  122.         if ($options && function_exists('stream_context_create')) {
  123.             if ($this->timeout{
  124.                 $timeout $this->timeout;
  125.             else {
  126.                 $timeout = 0;
  127.             }
  128.             $context stream_context_create($options);
  129.             $fp @$openfunc($this->addr$this->port$errno$errstr$timeout$context);
  130.         else {
  131.             if ($this->timeout{
  132.                 $fp @$openfunc($this->addr$this->port$errno$errstr$this->timeout);
  133.             else {
  134.                 $fp @$openfunc($this->addr$this->port$errno$errstr);
  135.             }
  136.         }
  137.  
  138.         if (!$fp{
  139.             return $this->raiseError($errstr$errno);
  140.         }
  141.  
  142.         $this->fp $fp;
  143.  
  144.         return $this->setBlocking($this->blocking);
  145.     }
  146.  
  147.     /**
  148.      * Disconnects from the peer, closes the socket.
  149.      *
  150.      * @access public
  151.      * @return mixed true on success or an error object otherwise
  152.      */
  153.     function disconnect()
  154.     {
  155.         if (is_resource($this->fp)) {
  156.             @fclose($this->fp);
  157.             $this->fp = null;
  158.             return true;
  159.         }
  160.         return $this->raiseError('not connected');
  161.     }
  162.  
  163.     /**
  164.      * Find out if the socket is in blocking mode.
  165.      *
  166.      * @access public
  167.      * @return boolean  The current blocking mode.
  168.      */
  169.     function isBlocking()
  170.     {
  171.         return $this->blocking;
  172.     }
  173.  
  174.     /**
  175.      * Sets whether the socket connection should be blocking or
  176.      * not. A read call to a non-blocking socket will return immediately
  177.      * if there is no data available, whereas it will block until there
  178.      * is data for blocking sockets.
  179.      *
  180.      * @param boolean $mode  True for blocking sockets, false for nonblocking.
  181.      * @access public
  182.      * @return mixed true on success or an error object otherwise
  183.      */
  184.     function setBlocking($mode)
  185.     {
  186.         if (is_resource($this->fp)) {
  187.             $this->blocking $mode;
  188.             socket_set_blocking($this->fp$this->blocking);
  189.             return true;
  190.         }
  191.         return $this->raiseError('not connected');
  192.     }
  193.  
  194.     /**
  195.      * Sets the timeout value on socket descriptor,
  196.      * expressed in the sum of seconds and microseconds
  197.      *
  198.      * @param integer $seconds  Seconds.
  199.      * @param integer $microseconds  Microseconds.
  200.      * @access public
  201.      * @return mixed true on success or an error object otherwise
  202.      */
  203.     function setTimeout($seconds$microseconds)
  204.     {
  205.         if (is_resource($this->fp)) {
  206.             socket_set_timeout($this->fp$seconds$microseconds);
  207.             return true;
  208.         }
  209.         return $this->raiseError('not connected');
  210.     }
  211.  
  212.     /**
  213.      * Returns information about an existing socket resource.
  214.      * Currently returns four entries in the result array:
  215.      *
  216.      * <p>
  217.      * timed_out (bool) - The socket timed out waiting for data<br>
  218.      * blocked (bool) - The socket was blocked<br>
  219.      * eof (bool) - Indicates EOF event<br>
  220.      * unread_bytes (int) - Number of bytes left in the socket buffer<br>
  221.      * </p>
  222.      *
  223.      * @access public
  224.      * @return mixed Array containing information about existing socket resource or an error object otherwise
  225.      */
  226.     function getStatus()
  227.     {
  228.         if (is_resource($this->fp)) {
  229.             return socket_get_status($this->fp);
  230.         }
  231.         return $this->raiseError('not connected');
  232.     }
  233.  
  234.     /**
  235.      * Get a specified line of data
  236.      *
  237.      * @access public
  238.      * @return $size bytes of data from the socket, or a PEAR_Error if
  239.      *          not connected.
  240.      */
  241.     function gets($size)
  242.     {
  243.         if (is_resource($this->fp)) {
  244.             return @fgets($this->fp$size);
  245.         }
  246.         return $this->raiseError('not connected');
  247.     }
  248.  
  249.     /**
  250.      * Read a specified amount of data. This is guaranteed to return,
  251.      * and has the added benefit of getting everything in one fread()
  252.      * chunk; if you know the size of the data you're getting
  253.      * beforehand, this is definitely the way to go.
  254.      *
  255.      * @param integer $size  The number of bytes to read from the socket.
  256.      * @access public
  257.      * @return $size bytes of data from the socket, or a PEAR_Error if
  258.      *          not connected.
  259.      */
  260.     function read($size)
  261.     {
  262.         if (is_resource($this->fp)) {
  263.             return @fread($this->fp$size);
  264.         }
  265.         return $this->raiseError('not connected');
  266.     }
  267.  
  268.     /**
  269.      * Write a specified amount of data.
  270.      *
  271.      * @access public
  272.      * @return mixed true on success or an error object otherwise
  273.      */
  274.     function write($data)
  275.     {
  276.         if (is_resource($this->fp)) {
  277.             return fwrite($this->fp$data);
  278.         }
  279.         return $this->raiseError('not connected');
  280.     }
  281.  
  282.     /**
  283.      * Write a line of data to the socket, followed by a trailing "\r\n".
  284.      *
  285.      * @access public
  286.      * @return mixed fputs result, or an error
  287.      */
  288.     function writeLine ($data)
  289.     {
  290.         if (is_resource($this->fp)) {
  291.             return fwrite($this->fp$data "\r\n");
  292.         }
  293.         return $this->raiseError('not connected');
  294.     }
  295.  
  296.     /**
  297.      * Tests for end-of-file on a socket descriptor.
  298.      *
  299.      * @access public
  300.      * @return bool 
  301.      */
  302.     function eof()
  303.     {
  304.         return (is_resource($this->fp&& feof($this->fp));
  305.     }
  306.  
  307.     /**
  308.      * Reads a byte of data
  309.      *
  310.      * @access public
  311.      * @return byte of data from the socket, or a PEAR_Error if
  312.      *          not connected.
  313.      */
  314.     function readByte()
  315.     {
  316.         if (is_resource($this->fp)) {
  317.             return ord(@fread($this->fp1));
  318.         }
  319.         return $this->raiseError('not connected');
  320.     }
  321.  
  322.     /**
  323.      * Reads a word of data
  324.      *
  325.      * @access public
  326.      * @return word of data from the socket, or a PEAR_Error if
  327.      *          not connected.
  328.      */
  329.     function readWord()
  330.     {
  331.         if (is_resource($this->fp)) {
  332.             $buf @fread($this->fp2);
  333.             return (ord($buf[0](ord($buf[1]<< 8));
  334.         }
  335.         return $this->raiseError('not connected');
  336.     }
  337.  
  338.     /**
  339.      * Reads an int of data
  340.      *
  341.      * @access public
  342.      * @return integer  1 int of data from the socket, or a PEAR_Error if
  343.      *                   not connected.
  344.      */
  345.     function readInt()
  346.     {
  347.         if (is_resource($this->fp)) {
  348.             $buf @fread($this->fp4);
  349.             return (ord($buf[0](ord($buf[1]<< 8+
  350.                     (ord($buf[2]<< 16(ord($buf[3]<< 24));
  351.         }
  352.         return $this->raiseError('not connected');
  353.     }
  354.  
  355.     /**
  356.      * Reads a zeroterminated string of data
  357.      *
  358.      * @access public
  359.      * @return string, or a PEAR_Error if
  360.      *          not connected.
  361.      */
  362.     function readString()
  363.     {
  364.         if (is_resource($this->fp)) {
  365.             $string '';
  366.             while (($char @fread($this->fp1)) != "\x00")  {
  367.                 $string .= $char;
  368.             }
  369.             return $string;
  370.         }
  371.         return $this->raiseError('not connected');
  372.     }
  373.  
  374.     /**
  375.      * Reads an IP Address and returns it in a dot formated string
  376.      *
  377.      * @access public
  378.      * @return Dot formated string, or a PEAR_Error if
  379.      *          not connected.
  380.      */
  381.     function readIPAddress()
  382.     {
  383.         if (is_resource($this->fp)) {
  384.             $buf @fread($this->fp4);
  385.             return sprintf("%s.%s.%s.%s"ord($buf[0])ord($buf[1]),
  386.                            ord($buf[2])ord($buf[3]));
  387.         }
  388.         return $this->raiseError('not connected');
  389.     }
  390.  
  391.     /**
  392.      * Read until either the end of the socket or a newline, whichever
  393.      * comes first. Strips the trailing newline from the returned data.
  394.      *
  395.      * @access public
  396.      * @return All available data up to a newline, without that
  397.      *          newline, or until the end of the socket, or a PEAR_Error if
  398.      *          not connected.
  399.      */
  400.     function readLine()
  401.     {
  402.         if (is_resource($this->fp)) {
  403.             $line '';
  404.             $timeout time($this->timeout;
  405.             while (!feof($this->fp&& (!$this->timeout || time($timeout)) {
  406.                 $line .= @fgets($this->fp$this->lineLength);
  407.                 if (substr($line-2== "\r\n" ||
  408.                     substr($line-1== "\n"{
  409.                     return rtrim($line"\r\n");
  410.                 }
  411.             }
  412.             return $line;
  413.         }
  414.         return $this->raiseError('not connected');
  415.     }
  416.  
  417.     /**
  418.      * Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
  419.      * socket is in blocking mode until the socket closes.
  420.      *
  421.      * @access public
  422.      * @return All data until the socket closes, or a PEAR_Error if
  423.      *          not connected.
  424.      */
  425.     function readAll()
  426.     {
  427.         if (is_resource($this->fp)) {
  428.             $data '';
  429.             while (!feof($this->fp)) {
  430.                 $data .= @fread($this->fp$this->lineLength);
  431.             }
  432.             return $data;
  433.         }
  434.         return $this->raiseError('not connected');
  435.     }
  436.  
  437.     /**
  438.      * Runs the equivalent of the select() system call on the socket
  439.      * with a timeout specified by tv_sec and tv_usec.
  440.      *
  441.      * @param integer $state    Which of read/write/error to check for.
  442.      * @param integer $tv_sec   Number of seconds for timeout.
  443.      * @param integer $tv_usec  Number of microseconds for timeout.
  444.      *
  445.      * @access public
  446.      * @return False if select fails, integer describing which of read/write/error
  447.      *          are ready, or PEAR_Error if not connected.
  448.      */
  449.     function select($state$tv_sec$tv_usec = 0)
  450.     {
  451.         if (is_resource($this->fp)) {
  452.             $read   = null;
  453.             $write  = null;
  454.             $except = null;
  455.             if ($state NET_SOCKET_READ{
  456.                 $read[$this->fp;
  457.             }
  458.             if ($state NET_SOCKET_WRITE{
  459.                 $write[$this->fp;
  460.             }
  461.             if ($state NET_SOCKET_ERROR{
  462.                 $except[$this->fp;
  463.             }
  464.             if (false === ($sr stream_select($read$write$except$tv_sec$tv_usec))) {
  465.                 return false;
  466.             }
  467.  
  468.             $result = 0;
  469.             if (count($read)) {
  470.                 $result |= NET_SOCKET_READ;
  471.             }
  472.             if (count($write)) {
  473.                 $result |= NET_SOCKET_WRITE;
  474.             }
  475.             if (count($except)) {
  476.                 $result |= NET_SOCKET_ERROR;
  477.             }
  478.             return $result;
  479.         }
  480.  
  481.         return $this->raiseError('not connected');
  482.     }
  483.  
  484. }

Documentation generated on Mon, 11 Mar 2019 13:59:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.