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

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