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.9 2004/04/26 23:02:41 chagenbu Exp $
* Generalized Socket class. More docs to be written.
* @author Stig Bakken <ssb@php.net>
* @author Chuck Hagenbuch <chuck@horde.org>
class Net_Socket extends PEAR {
/** Socket file pointer. */
/** Whether the socket is blocking. */
/** Whether the socket is 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. */
/** Number of bytes to read at a time in readLine() and
* Constructs a new Net_Socket object.
* Connect to the specified port. If called when the socket is
* already connected, it disconnects and connects again.
* @param $addr string IP address or host name
* @param $port int TCP port number
* @param $persistent bool (optional) whether the connection is
* persistent (kept open between requests by the web server)
* @param $timeout int (optional) how long to wait for data
* @param $options array see options for stream_context_create
* @return mixed true on success or error object
function connect ($addr, $port, $persistent = null , $timeout = null , $options = null )
$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 bool 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 $mode bool 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 $seconds int seconds
* @param $microseconds int 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 $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 $this->write ($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 ord($this->read (1 ));
return $this->raiseError ("not connected");
* @return 1 word of data from the socket, or a PEAR_Error if
return (ord($buf[0 ]) + (ord($buf[1 ]) << 8 ));
return $this->raiseError ("not connected");
* @return 1 int of data from the socket, or a PEAR_Error if
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 = $this->read (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
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 (!$this->eof () && (!$this->timeout || time() < $timeout)) {
$line .= $this->gets ($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
$data .= $this->read ($this->lineLength);
return $this->raiseError ("not connected");
Documentation generated on Mon, 11 Mar 2019 10:15:57 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|