ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Net_Socket Class Reference

Generalized Socket class. More...

+ Inheritance diagram for Net_Socket:
+ Collaboration diagram for Net_Socket:

Public Member Functions

 connect ($addr, $port=0, $persistent=null, $timeout=null, $options=null)
 Connect to the specified port. More...
 
 disconnect ()
 Disconnects from the peer, closes the socket. More...
 
 isBlocking ()
 Find out if the socket is in blocking mode. More...
 
 setBlocking ($mode)
 Sets whether the socket connection should be blocking or not. More...
 
 setTimeout ($seconds, $microseconds)
 Sets the timeout value on socket descriptor, expressed in the sum of seconds and microseconds. More...
 
 setWriteBuffer ($size)
 Sets the file buffering size on the stream. More...
 
 getStatus ()
 Returns information about an existing socket resource. More...
 
 gets ($size)
 Get a specified line of data. More...
 
 read ($size)
 Read a specified amount of data. More...
 
 write ($data, $blocksize=null)
 Write a specified amount of data. More...
 
 writeLine ($data)
 Write a line of data to the socket, followed by a trailing "\r\n". More...
 
 eof ()
 Tests for end-of-file on a socket descriptor. More...
 
 readByte ()
 Reads a byte of data. More...
 
 readWord ()
 Reads a word of data. More...
 
 readInt ()
 Reads an int of data. More...
 
 readString ()
 Reads a zero-terminated string of data. More...
 
 readIPAddress ()
 Reads an IP Address and returns it in a dot formatted string. More...
 
 readLine ()
 Read until either the end of the socket or a newline, whichever comes first. More...
 
 readAll ()
 Read until the socket closes, or until there is no more data in the inner PHP buffer. More...
 
 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 tv_usec. More...
 
 enableCrypto ($enabled, $type)
 Turns encryption on/off on a connected socket. More...
 
- Public Member Functions inherited from PEAR
 PEAR ($error_class=null)
 Constructor. More...
 
 _PEAR ()
 Destructor (the emulated type of...). More...
 
getStaticProperty ($class, $var)
 If you have a class that's mostly/entirely static, and you need static properties, you can use this method to simulate them. More...
 
 registerShutdownFunc ($func, $args=array())
 Use this function to register a shutdown method for static classes. More...
 
 isError ($data, $code=null)
 Tell whether a value is a PEAR error. More...
 
 setErrorHandling ($mode=null, $options=null)
 Sets how errors generated by this object should be handled. More...
 
 expectError ($code=' *')
 This method is used to tell which errors you expect to get. More...
 
 popExpect ()
 This method pops one element off the expected error codes stack. More...
 
 _checkDelExpect ($error_code)
 This method checks unsets an error code if available. More...
 
 delExpect ($error_code)
 This method deletes all occurences of the specified element from the expected error codes stack. More...
 
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 default error handling applied. More...
 
throwError ($message=null, $code=null, $userinfo=null)
 Simpler form of raiseError with fewer options. More...
 
 staticPushErrorHandling ($mode, $options=null)
 
 staticPopErrorHandling ()
 
 pushErrorHandling ($mode, $options=null)
 Push a new error handler on top of the error handler options stack. More...
 
 popErrorHandling ()
 Pop the last error handler used. More...
 
 loadExtension ($ext)
 OS independant PHP extension load. More...
 

Data Fields

 $fp = null
 Socket file pointer. More...
 
 $blocking = true
 Whether the socket is blocking. More...
 
 $persistent = false
 Whether the socket is persistent. More...
 
 $addr = ''
 The IP address to connect to. More...
 
 $port = 0
 The port number to connect to. More...
 
 $timeout = false
 Number of seconds to wait on socket connections before assuming there's no more data. More...
 
 $lineLength = 2048
 Number of bytes to read at a time in readLine() and readAll(). More...
 
- Data Fields inherited from PEAR
 $_debug = false
 
 $_default_error_mode = null
 
 $_default_error_options = null
 
 $_default_error_handler = ''
 
 $_error_class = 'PEAR_Error'
 
 $_expected_errors = array()
 

Detailed Description

Generalized Socket class.

Version
1.1
Author
Stig Bakken ssb@p.nosp@m.hp.n.nosp@m.et
Chuck Hagenbuch chuck.nosp@m.@hor.nosp@m.de.or.nosp@m.g

Definition at line 35 of file Socket.php.

Member Function Documentation

◆ connect()

Net_Socket::connect (   $addr,
  $port = 0,
  $persistent = null,
  $timeout = null,
  $options = null 
)

Connect to the specified port.

If called when the socket is already connected, it disconnects and connects again.

Parameters
string$addrIP address or host name.
integer$portTCP port number.
boolean$persistent(optional) Whether the connection is persistent (kept open between requests by the web server).
integer$timeout(optional) How long to wait for data.
array$optionsSee options for stream_context_create.

@access public

Returns
boolean | PEAR_Error True on success or a PEAR_Error on failure.

Definition at line 97 of file Socket.php.

98 {
99 if (is_resource($this->fp)) {
100 @fclose($this->fp);
101 $this->fp = null;
102 }
103
104 if (!$addr) {
105 return $this->raiseError('$addr cannot be empty');
106 } elseif (strspn($addr, '.0123456789') == strlen($addr) ||
107 strstr($addr, '/') !== false) {
108 $this->addr = $addr;
109 } else {
110 $this->addr = @gethostbyname($addr);
111 }
112
113 $this->port = $port % 65536;
114
115 if ($persistent !== null) {
116 $this->persistent = $persistent;
117 }
118
119 if ($timeout !== null) {
120 $this->timeout = $timeout;
121 }
122
123 $openfunc = $this->persistent ? 'pfsockopen' : 'fsockopen';
124 $errno = 0;
125 $errstr = '';
126 $old_track_errors = @ini_set('track_errors', 1);
127 if ($options && function_exists('stream_context_create')) {
128 if ($this->timeout) {
130 } else {
131 $timeout = 0;
132 }
133 $context = stream_context_create($options);
134
135 // Since PHP 5 fsockopen doesn't allow context specification
136 if (function_exists('stream_socket_client')) {
137 $flags = $this->persistent ? STREAM_CLIENT_PERSISTENT : STREAM_CLIENT_CONNECT;
138 $addr = $this->addr . ':' . $this->port;
139 $fp = stream_socket_client($addr, $errno, $errstr, $timeout, $flags, $context);
140 } else {
141 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $timeout, $context);
142 }
143 } else {
144 if ($this->timeout) {
145 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr, $this->timeout);
146 } else {
147 $fp = @$openfunc($this->addr, $this->port, $errno, $errstr);
148 }
149 }
150
151 if (!$fp) {
152 if ($errno == 0 && isset($php_errormsg)) {
153 $errstr = $php_errormsg;
154 }
155 @ini_set('track_errors', $old_track_errors);
156 return $this->raiseError($errstr, $errno);
157 }
158
159 @ini_set('track_errors', $old_track_errors);
160 $this->fp = $fp;
161
162 return $this->setBlocking($this->blocking);
163 }
setBlocking($mode)
Sets whether the socket connection should be blocking or not.
Definition: Socket.php:203
$port
The port number to connect to.
Definition: Socket.php:65
$addr
The IP address to connect to.
Definition: Socket.php:59
$fp
Socket file pointer.
Definition: Socket.php:41
$timeout
Number of seconds to wait on socket connections before assuming there's no more data.
Definition: Socket.php:72
$persistent
Whether the socket is persistent.
Definition: Socket.php:53
& 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...
Definition: PEAR.php:524
if(!is_array($argv)) $options

References $addr, $fp, $options, $persistent, $port, $timeout, PEAR\raiseError(), and setBlocking().

+ Here is the call graph for this function:

◆ disconnect()

Net_Socket::disconnect ( )

Disconnects from the peer, closes the socket.

@access public

Returns
mixed true on success or a PEAR_Error instance otherwise

Definition at line 171 of file Socket.php.

172 {
173 if (!is_resource($this->fp)) {
174 return $this->raiseError('not connected');
175 }
176
177 @fclose($this->fp);
178 $this->fp = null;
179 return true;
180 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ enableCrypto()

Net_Socket::enableCrypto (   $enabled,
  $type 
)

Turns encryption on/off on a connected socket.

Parameters
bool$enabledSet this parameter to true to enable encryption and false to disable encryption.
integer$typeType of encryption. See http://se.php.net/manual/en/function.stream-socket-enable-crypto.php for values.

@access public

Returns
false on error, true on success and 0 if there isn't enough data and the user should try again (non-blocking sockets only). A PEAR_Error object is returned if the socket is not connected

Definition at line 580 of file Socket.php.

581 {
582 if (version_compare(phpversion(), "5.1.0", ">=")) {
583 if (!is_resource($this->fp)) {
584 return $this->raiseError('not connected');
585 }
586 return @stream_socket_enable_crypto($this->fp, $enabled, $type);
587 } else {
588 return $this->raiseError('Net_Socket::enableCrypto() requires php version >= 5.1.0');
589 }
590 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ eof()

Net_Socket::eof ( )

Tests for end-of-file on a socket descriptor.

Also returns true if the socket is disconnected.

@access public

Returns
bool

Definition at line 374 of file Socket.php.

375 {
376 return (!is_resource($this->fp) || feof($this->fp));
377 }

◆ gets()

Net_Socket::gets (   $size)

Get a specified line of data.

@access public

Returns
$size bytes of data from the socket, or a PEAR_Error if not connected.

Definition at line 283 of file Socket.php.

284 {
285 if (!is_resource($this->fp)) {
286 return $this->raiseError('not connected');
287 }
288
289 return @fgets($this->fp, $size);
290 }
$size
Definition: RandomTest.php:79

References $size, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ getStatus()

Net_Socket::getStatus ( )

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
blocked (bool) - The socket was blocked
eof (bool) - Indicates EOF event
unread_bytes (int) - Number of bytes left in the socket buffer

@access public

Returns
mixed Array containing information about existing socket resource or a PEAR_Error instance otherwise

Definition at line 267 of file Socket.php.

268 {
269 if (!is_resource($this->fp)) {
270 return $this->raiseError('not connected');
271 }
272
273 return socket_get_status($this->fp);
274 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ isBlocking()

Net_Socket::isBlocking ( )

Find out if the socket is in blocking mode.

@access public

Returns
boolean The current blocking mode.

Definition at line 188 of file Socket.php.

189 {
190 return $this->blocking;
191 }
$blocking
Whether the socket is blocking.
Definition: Socket.php:47

References $blocking.

◆ read()

Net_Socket::read (   $size)

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.

Parameters
integer$sizeThe number of bytes to read from the socket. @access public
Returns
$size bytes of data from the socket, or a PEAR_Error if not connected.

Definition at line 303 of file Socket.php.

304 {
305 if (!is_resource($this->fp)) {
306 return $this->raiseError('not connected');
307 }
308
309 return @fread($this->fp, $size);
310 }

References $size, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ readAll()

Net_Socket::readAll ( )

Read until the socket closes, or until there is no more data in the inner PHP buffer.

If the inner buffer is empty, in blocking mode we wait for at least 1 byte of data. Therefore, in blocking mode, if there is no data at all to be read, this function will never exit (unless the socket is closed on the remote end).

@access public

Returns
string All data until the socket closes, or a PEAR_Error if not connected.

Definition at line 507 of file Socket.php.

508 {
509 if (!is_resource($this->fp)) {
510 return $this->raiseError('not connected');
511 }
512
513 $data = '';
514 while (!feof($this->fp)) {
515 $data .= @fread($this->fp, $this->lineLength);
516 }
517 return $data;
518 }
$data

References $data, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ readByte()

Net_Socket::readByte ( )

Reads a byte of data.

@access public

Returns
1 byte of data from the socket, or a PEAR_Error if not connected.

Definition at line 386 of file Socket.php.

387 {
388 if (!is_resource($this->fp)) {
389 return $this->raiseError('not connected');
390 }
391
392 return ord(@fread($this->fp, 1));
393 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ readInt()

Net_Socket::readInt ( )

Reads an int of data.

@access public

Returns
integer 1 int of data from the socket, or a PEAR_Error if not connected.

Definition at line 419 of file Socket.php.

420 {
421 if (!is_resource($this->fp)) {
422 return $this->raiseError('not connected');
423 }
424
425 $buf = @fread($this->fp, 4);
426 return (ord($buf[0]) + (ord($buf[1]) << 8) +
427 (ord($buf[2]) << 16) + (ord($buf[3]) << 24));
428 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ readIPAddress()

Net_Socket::readIPAddress ( )

Reads an IP Address and returns it in a dot formatted string.

@access public

Returns
Dot formatted string, or a PEAR_Error if not connected.

Definition at line 457 of file Socket.php.

458 {
459 if (!is_resource($this->fp)) {
460 return $this->raiseError('not connected');
461 }
462
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]));
466 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ readLine()

Net_Socket::readLine ( )

Read until either the end of the socket or a newline, whichever comes first.

Strips the trailing newline from the returned data.

@access public

Returns
All available data up to a newline, without that newline, or until the end of the socket, or a PEAR_Error if not connected.

Definition at line 477 of file Socket.php.

478 {
479 if (!is_resource($this->fp)) {
480 return $this->raiseError('not connected');
481 }
482
483 $line = '';
484 $timeout = time() + $this->timeout;
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");
489 }
490 }
491 return $line;
492 }

References $timeout, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ readString()

Net_Socket::readString ( )

Reads a zero-terminated string of data.

@access public

Returns
string, or a PEAR_Error if not connected.

Definition at line 437 of file Socket.php.

438 {
439 if (!is_resource($this->fp)) {
440 return $this->raiseError('not connected');
441 }
442
443 $string = '';
444 while (($char = @fread($this->fp, 1)) != "\x00") {
445 $string .= $char;
446 }
447 return $string;
448 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ readWord()

Net_Socket::readWord ( )

Reads a word of data.

@access public

Returns
1 word of data from the socket, or a PEAR_Error if not connected.

Definition at line 402 of file Socket.php.

403 {
404 if (!is_resource($this->fp)) {
405 return $this->raiseError('not connected');
406 }
407
408 $buf = @fread($this->fp, 2);
409 return (ord($buf[0]) + (ord($buf[1]) << 8));
410 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ select()

Net_Socket::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 tv_usec.

Parameters
integer$stateWhich of read/write/error to check for.
integer$tv_secNumber of seconds for timeout.
integer$tv_usecNumber of microseconds for timeout.

@access public

Returns
False if select fails, integer describing which of read/write/error are ready, or PEAR_Error if not connected.

Definition at line 532 of file Socket.php.

533 {
534 if (!is_resource($this->fp)) {
535 return $this->raiseError('not connected');
536 }
537
538 $read = null;
539 $write = null;
540 $except = null;
541 if ($state & NET_SOCKET_READ) {
542 $read[] = $this->fp;
543 }
544 if ($state & NET_SOCKET_WRITE) {
545 $write[] = $this->fp;
546 }
547 if ($state & NET_SOCKET_ERROR) {
548 $except[] = $this->fp;
549 }
550 if (false === ($sr = stream_select($read, $write, $except, $tv_sec, $tv_usec))) {
551 return false;
552 }
553
554 $result = 0;
555 if (count($read)) {
557 }
558 if (count($write)) {
560 }
561 if (count($except)) {
563 }
564 return $result;
565 }
$result
const NET_SOCKET_ERROR
Definition: Socket.php:26
const NET_SOCKET_READ
Definition: Socket.php:24
const NET_SOCKET_WRITE
Definition: Socket.php:25

References $fp, $result, NET_SOCKET_ERROR, NET_SOCKET_READ, NET_SOCKET_WRITE, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ setBlocking()

Net_Socket::setBlocking (   $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.

Parameters
boolean$modeTrue for blocking sockets, false for nonblocking. @access public
Returns
mixed true on success or a PEAR_Error instance otherwise

Definition at line 203 of file Socket.php.

204 {
205 if (!is_resource($this->fp)) {
206 return $this->raiseError('not connected');
207 }
208
209 $this->blocking = $mode;
210 socket_set_blocking($this->fp, $this->blocking);
211 return true;
212 }

References PEAR\raiseError().

Referenced by connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setTimeout()

Net_Socket::setTimeout (   $seconds,
  $microseconds 
)

Sets the timeout value on socket descriptor, expressed in the sum of seconds and microseconds.

Parameters
integer$secondsSeconds.
integer$microsecondsMicroseconds. @access public
Returns
mixed true on success or a PEAR_Error instance otherwise

Definition at line 223 of file Socket.php.

224 {
225 if (!is_resource($this->fp)) {
226 return $this->raiseError('not connected');
227 }
228
229 return socket_set_timeout($this->fp, $seconds, $microseconds);
230 }

References PEAR\raiseError().

+ Here is the call graph for this function:

◆ setWriteBuffer()

Net_Socket::setWriteBuffer (   $size)

Sets the file buffering size on the stream.

See php's stream_set_write_buffer for more information.

Parameters
integer$sizeWrite buffer size. @access public
Returns
mixed on success or an PEAR_Error object otherwise

Definition at line 240 of file Socket.php.

241 {
242 if (!is_resource($this->fp)) {
243 return $this->raiseError('not connected');
244 }
245
246 $returned = stream_set_write_buffer($this->fp, $size);
247 if ($returned == 0) {
248 return true;
249 }
250 return $this->raiseError('Cannot set write buffer.');
251 }

References $size, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ write()

Net_Socket::write (   $data,
  $blocksize = null 
)

Write a specified amount of data.

Parameters
string$dataData to write.
integer$blocksizeAmount of data to write at once. NULL means all at once.

@access public

Returns
mixed If the socket is not connected, returns an instance of PEAR_Error If the write succeeds, returns the number of bytes written If the write fails, returns false.

Definition at line 324 of file Socket.php.

325 {
326 if (!is_resource($this->fp)) {
327 return $this->raiseError('not connected');
328 }
329
330 if (is_null($blocksize) && !OS_WINDOWS) {
331 return @fwrite($this->fp, $data);
332 } else {
333 if (is_null($blocksize)) {
334 $blocksize = 1024;
335 }
336
337 $pos = 0;
338 $size = strlen($data);
339 while ($pos < $size) {
340 $written = @fwrite($this->fp, substr($data, $pos, $blocksize));
341 if ($written === false) {
342 return false;
343 }
344 $pos += $written;
345 }
346
347 return $pos;
348 }
349 }

References $data, $size, and PEAR\raiseError().

+ Here is the call graph for this function:

◆ writeLine()

Net_Socket::writeLine (   $data)

Write a line of data to the socket, followed by a trailing "\r\n".

@access public

Returns
mixed fputs result, or an error

Definition at line 357 of file Socket.php.

358 {
359 if (!is_resource($this->fp)) {
360 return $this->raiseError('not connected');
361 }
362
363 return fwrite($this->fp, $data . "\r\n");
364 }

References $data, and PEAR\raiseError().

+ Here is the call graph for this function:

Field Documentation

◆ $addr

string Net_Socket::$addr = ''

The IP address to connect to.

Definition at line 59 of file Socket.php.

Referenced by connect().

◆ $blocking

boolean Net_Socket::$blocking = true

Whether the socket is blocking.

Defaults to true.

Definition at line 47 of file Socket.php.

Referenced by isBlocking().

◆ $fp

resource Net_Socket::$fp = null

Socket file pointer.

Definition at line 41 of file Socket.php.

Referenced by connect(), and select().

◆ $lineLength

integer Net_Socket::$lineLength = 2048

Number of bytes to read at a time in readLine() and readAll().

Defaults to 2048.

Definition at line 79 of file Socket.php.

◆ $persistent

boolean Net_Socket::$persistent = false

Whether the socket is persistent.

Defaults to false.

Definition at line 53 of file Socket.php.

Referenced by connect().

◆ $port

integer Net_Socket::$port = 0

The port number to connect to.

Definition at line 65 of file Socket.php.

Referenced by connect().

◆ $timeout

integer Net_Socket::$timeout = false

Number of seconds to wait on socket connections before assuming there's no more data.

Defaults to no timeout.

Definition at line 72 of file Socket.php.

Referenced by connect(), and readLine().


The documentation for this class was generated from the following file: