Source for file Socket.php
Documentation is available at Socket.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@php.net> |
// | Chuck Hagenbuch <chuck@horde.org> |
// +----------------------------------------------------------------------+
// $Id: Socket.php,v 1.21 2004/12/13 21:45:59 chagenbu Exp $
define('NET_SOCKET_WRITE', 2 );
define('NET_SOCKET_ERROR', 3 );
* Generalized Socket class.
* @author Stig Bakken <ssb@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
class Net_Socket extends PEAR {
* Whether the socket is blocking. Defaults to true.
* Whether the socket is persistent. Defaults to false.
* @var boolean $persistent
* The IP address to connect to.
* The port number to connect to.
* Number of seconds to wait on socket connections before assuming
* there's no more data. Defaults to no timeout.
* Number of bytes to read at a time in readLine() and
* readAll(). Defaults to 2048.
* @var integer $lineLength
* Connect to the specified port. If called when the socket is
* already connected, it disconnects and connects again.
* @param string $addr IP address or host name.
* @param integer $port TCP port number.
* @param boolean $persistent (optional) Whether the connection is
* persistent (kept open between requests
* @param integer $timeout (optional) How long to wait for data.
* @param array $options See options for stream_context_create.
* @return mixed true on success or error object
function connect ($addr, $port = 0 , $persistent = null , $timeout = null , $options = null )
return $this->raiseError ('$addr cannot be empty');
if (strcmp($this->addr, $addr) == 0 ) {
return $this->raiseError (" unable to lookup hostname '$addr'" );
$this->port = $port % 65536;
if ($persistent !== null ) {
$this->persistent = $persistent;
$this->timeout = $timeout;
$openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
$timeout = $this->timeout;
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
$fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
return $this->raiseError ($errstr, $errno);
return $this->setBlocking ($this->blocking);
* Disconnects from the peer, closes the socket.
* @return mixed true on success or an error object otherwise
return $this->raiseError ('not connected');
* Find out if the socket is in blocking mode.
* @return boolean The current blocking mode.
* Sets whether the socket connection should be blocking or
* not. A read call to a non-blocking socket will return immediately
* if there is no data available, whereas it will block until there
* is data for blocking sockets.
* @param boolean $mode True for blocking sockets, false for nonblocking.
* @return mixed true on success or an error object otherwise
function setBlocking ($mode)
return $this->raiseError ('not connected');
* Sets the timeout value on socket descriptor,
* expressed in the sum of seconds and microseconds
* @param integer $seconds Seconds.
* @param integer $microseconds Microseconds.
* @return mixed true on success or an error object otherwise
function setTimeout ($seconds, $microseconds)
return $this->raiseError ('not connected');
* Returns information about an existing socket resource.
* Currently returns four entries in the result array:
* timed_out (bool) - The socket timed out waiting for data<br>
* blocked (bool) - The socket was blocked<br>
* eof (bool) - Indicates EOF event<br>
* unread_bytes (int) - Number of bytes left in the socket buffer<br>
* @return mixed Array containing information about existing socket resource or an error object otherwise
return $this->raiseError ('not connected');
* Get a specified line of data
* @return $size bytes of data from the socket, or a PEAR_Error if
return @fgets($this->fp, $size);
return $this->raiseError ('not connected');
* Read a specified amount of data. This is guaranteed to return,
* and has the added benefit of getting everything in one fread()
* chunk; if you know the size of the data you're getting
* beforehand, this is definitely the way to go.
* @param integer $size The number of bytes to read from the socket.
* @return $size bytes of data from the socket, or a PEAR_Error if
return @fread($this->fp, $size);
return $this->raiseError ('not connected');
* Write a specified amount of data.
* @return mixed true on success or an error object otherwise
return fwrite($this->fp, $data);
return $this->raiseError ('not connected');
* Write a line of data to the socket, followed by a trailing "\r\n".
* @return mixed fputs result, or an error
function writeLine ($data)
return fwrite($this->fp, $data . "\r\n");
return $this->raiseError ('not connected');
* Tests for end-of-file on a socket descriptor.
* @return 1 byte of data from the socket, or a PEAR_Error if
return $this->raiseError ('not connected');
* @return 1 word of data from the socket, or a PEAR_Error if
$buf = @fread($this->fp, 2 );
return (ord($buf[0 ]) + (ord($buf[1 ]) << 8 ));
return $this->raiseError ('not connected');
* @return integer 1 int of data from the socket, or a PEAR_Error if
$buf = @fread($this->fp, 4 );
return (ord($buf[0 ]) + (ord($buf[1 ]) << 8 ) +
(ord($buf[2 ]) << 16 ) + (ord($buf[3 ]) << 24 ));
return $this->raiseError ('not connected');
* Reads a zeroterminated string of data
* @return string, or a PEAR_Error if
while (($char = @fread($this->fp, 1 )) != "\x00") {
return $this->raiseError ('not connected');
* Reads an IP Address and returns it in a dot formated string
* @return Dot formated string, or a PEAR_Error if
$buf = @fread($this->fp, 4 );
return $this->raiseError ('not connected');
* Read until either the end of the socket or a newline, whichever
* comes first. Strips the trailing newline from the returned data.
* @return All available data up to a newline, without that
* newline, or until the end of the socket, or a PEAR_Error if
$timeout = time() + $this->timeout;
while (!feof($this->fp) && (!$this->timeout || time() < $timeout)) {
$line .= @fgets($this->fp, $this->lineLength);
if (substr($line, -2 ) == "\r\n" ||
return rtrim($line, "\r\n");
return $this->raiseError ('not connected');
* Read until the socket closes. THIS FUNCTION WILL NOT EXIT if the
* socket is in blocking mode until the socket closes.
* @return All data until the socket closes, or a PEAR_Error if
while (!feof($this->fp)) {
$data .= @fread($this->fp, $this->lineLength);
return $this->raiseError ('not connected');
* Runs the equivalent of the select() system call on the socket
* with a timeout specified by tv_sec and tv_usec.
* @param integer $state Which of read/write/error to check for.
* @param integer $tv_sec Number of seconds for timeout.
* @param integer $tv_usec Number of microseconds for timeout.
* @return False if select fails, integer describing which of read/write/error
* are ready, or PEAR_Error if not connected.
function select ($state, $tv_sec, $tv_usec = 0 )
if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
return $this->raiseError ('not connected');
Documentation generated on Mon, 11 Mar 2019 13:59:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|