22 require_once
'PEAR.php';
24 define(
'NET_SOCKET_READ', 1);
25 define(
'NET_SOCKET_WRITE', 2);
26 define(
'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)) {
338 $size = strlen(
$data);
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');