22require_once
'PEAR.php';
24define(
'NET_SOCKET_READ', 1);
25define(
'NET_SOCKET_WRITE', 2);
26define(
'NET_SOCKET_ERROR', 4);
99 if (is_resource($this->fp)) {
105 return $this->
raiseError(
'$addr cannot be empty');
106 } elseif (strspn(
$addr,
'.0123456789') == strlen(
$addr) ||
107 strstr(
$addr,
'/') !==
false) {
110 $this->addr = @gethostbyname(
$addr);
113 $this->port =
$port % 65536;
123 $openfunc = $this->persistent ?
'pfsockopen' :
'fsockopen';
126 $old_track_errors = @ini_set(
'track_errors', 1);
127 if (
$options && function_exists(
'stream_context_create')) {
128 if ($this->timeout) {
133 $context = stream_context_create(
$options);
136 if (function_exists(
'stream_socket_client')) {
137 $flags = $this->persistent ? STREAM_CLIENT_PERSISTENT : STREAM_CLIENT_CONNECT;
139 $fp = stream_socket_client(
$addr, $errno, $errstr,
$timeout, $flags, $context);
141 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr,
$timeout, $context);
144 if ($this->timeout) {
145 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
147 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
152 if ($errno == 0 && isset($php_errormsg)) {
153 $errstr = $php_errormsg;
155 @ini_set(
'track_errors', $old_track_errors);
159 @ini_set(
'track_errors', $old_track_errors);
173 if (!is_resource($this->fp)) {
205 if (!is_resource($this->fp)) {
209 $this->blocking = $mode;
210 socket_set_blocking($this->fp, $this->blocking);
225 if (!is_resource($this->fp)) {
229 return socket_set_timeout($this->fp, $seconds, $microseconds);
242 if (!is_resource($this->fp)) {
246 $returned = stream_set_write_buffer($this->fp,
$size);
247 if ($returned == 0) {
250 return $this->
raiseError(
'Cannot set write buffer.');
269 if (!is_resource($this->fp)) {
273 return socket_get_status($this->fp);
285 if (!is_resource($this->fp)) {
289 return @fgets($this->fp,
$size);
305 if (!is_resource($this->fp)) {
309 return @fread($this->fp,
$size);
326 if (!is_resource($this->fp)) {
330 if (is_null($blocksize) && !OS_WINDOWS) {
331 return @fwrite($this->fp,
$data);
333 if (is_null($blocksize)) {
339 while ($pos <
$size) {
340 $written = @fwrite($this->fp, substr(
$data, $pos, $blocksize));
341 if ($written ===
false) {
359 if (!is_resource($this->fp)) {
363 return fwrite($this->fp,
$data .
"\r\n");
376 return (!is_resource($this->fp) || feof($this->fp));
388 if (!is_resource($this->fp)) {
392 return ord(@fread($this->fp, 1));
404 if (!is_resource($this->fp)) {
408 $buf = @fread($this->fp, 2);
409 return (ord($buf[0]) + (ord($buf[1]) << 8));
421 if (!is_resource($this->fp)) {
425 $buf = @fread($this->fp, 4);
426 return (ord($buf[0]) + (ord($buf[1]) << 8) +
427 (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
439 if (!is_resource($this->fp)) {
444 while (($char = @fread($this->fp, 1)) !=
"\x00") {
459 if (!is_resource($this->fp)) {
463 $buf = @fread($this->fp, 4);
464 return sprintf(
'%d.%d.%d.%d', ord($buf[0]), ord($buf[1]),
465 ord($buf[2]), ord($buf[3]));
479 if (!is_resource($this->fp)) {
485 while (!feof($this->fp) && (!$this->timeout || time() <
$timeout)) {
486 $line .= @fgets($this->fp, $this->lineLength);
487 if (substr($line, -1) ==
"\n") {
488 return rtrim($line,
"\r\n");
509 if (!is_resource($this->fp)) {
514 while (!feof($this->fp)) {
515 $data .= @fread($this->fp, $this->lineLength);
532 function select($state, $tv_sec, $tv_usec = 0)
534 if (!is_resource($this->fp)) {
550 if (
false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
561 if (count($except)) {
582 if (version_compare(phpversion(),
"5.1.0",
">=")) {
583 if (!is_resource($this->fp)) {
586 return @stream_socket_enable_crypto($this->fp, $enabled, $type);
588 return $this->
raiseError(
'Net_Socket::enableCrypto() requires php version >= 5.1.0');
Generalized Socket class.
read($size)
Read a specified amount of data.
eof()
Tests for end-of-file on a socket descriptor.
setBlocking($mode)
Sets whether the socket connection should be blocking or not.
gets($size)
Get a specified line of data.
disconnect()
Disconnects from the peer, closes the socket.
enableCrypto($enabled, $type)
Turns encryption on/off on a connected socket.
setWriteBuffer($size)
Sets the file buffering size on the stream.
writeLine($data)
Write a line of data to the socket, followed by a trailing "\r\n".
$port
The port number to connect to.
readWord()
Reads a word of data.
readByte()
Reads a byte of data.
isBlocking()
Find out if the socket is in blocking mode.
readIPAddress()
Reads an IP Address and returns it in a dot formatted string.
setTimeout($seconds, $microseconds)
Sets the timeout value on socket descriptor, expressed in the sum of seconds and microseconds.
readLine()
Read until either the end of the socket or a newline, whichever comes first.
select($state, $tv_sec, $tv_usec=0)
Runs the equivalent of the select() system call on the socket with a timeout specified by tv_sec and ...
$addr
The IP address to connect to.
connect($addr, $port=0, $persistent=null, $timeout=null, $options=null)
Connect to the specified port.
readInt()
Reads an int of data.
write($data, $blocksize=null)
Write a specified amount of data.
getStatus()
Returns information about an existing socket resource.
$timeout
Number of seconds to wait on socket connections before assuming there's no more data.
readAll()
Read until the socket closes, or until there is no more data in the inner PHP buffer.
$blocking
Whether the socket is blocking.
readString()
Reads a zero-terminated string of data.
$persistent
Whether the socket is persistent.
$lineLength
Number of bytes to read at a time in readLine() and readAll().
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
if(!is_array($argv)) $options