<?php
class t_socket {
#properties
protected $host = 'localhost';
protected $port = 80;
protected $username = '';
protected $passwd = '';
private $connected = false;
private $errno =0;
private $errstr = '';
public $timeout = 20;
protected $connex_timeout = 20;
public $buffer = '';
public $lastdata = '';
public $status = 0;
public $service_status =0;
#para socket client php5
public $protocol = 'tcp://'; #TCP o UDP
public $strhost = 'tcp://localhost:80';
#contiene el resource handle del socket creado
public static $connection = null;
#datos de la conexion:
public $bytes_read = 0;
public $bytes_write = 0;
#arrays
#single methods
function status() {
return $this->status;
}
function set_status($status) {
$this->status = $status;
}
function connected() {
return $this->connected;
}
function set_connected($status) {
$this->connected = $status;
}
#constructor
#Funcion constructora de la clase
function __construct($host='', $port='', $timeout='') {
$this->host = $host;
$this->port = $port;
$this->passwd = $password;
$this->status = array();
$this->connection =null;
$this->buffer = null;
$this->lastdata = '';
if ($timeout!='') {
$this->timeout = $timeout;
$this->connex_timeout = $timeout;
}
set_time_limit($this->timeout);
}
#destructor
function __destruct() {
$this->close();
unset($this->connection);
}
#methods
public function connect($persistent=false) {
if ($persistent) {
$this->connection = pfsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
} else {
$this->connection = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
}
if (!$this->connection) {
echo 'Error: ' . $this->errno . '<br /><b>' . $this->errstr . '</b>';
return false;
}
#defino el tiempo de espera de la conexion
stream_set_timeout($this->connection, $this->connex_timeout);
if ($this->connection) {
#procedo a hacer las evaluaciones necesarias del socket
$this->connected = true;
$this->service_status = 1;
#obtiene un array de estado, timed_out, blocked, eof (end of file), unread_bytes (cantidad de bytes en el buffer interno)
#mode (modo de acceso) y uri (direccion actual del recurso)
$this->status = stream_get_meta_data($this->connection);
return true;
} else {
return false;
}
}
public function close() {
if ($this->connected) {
fclose($this->connection);
$this->connected = false;
}
}
function read() {
if ($this->connected) {
socket_set_timeout($this->connection,0,100*1000);
$linecount = 0;
while ($line = fgets($this->connection)) {
$this->buffer[] = trim($line);
$linecount++;
}
$this->lastdata = $line;
$this->bytes_read = strlen($this->lastdata);
$this->status = stream_get_meta_data($this->connection);
return $linecount;
} else {
return false;
}
}
function write($data) {
if ($this->connected) {
$nbytes = fwrite($this->connection, $data, strlen($data));
if ($nbytes) {
$this->bytes_write = $nbytes;
return true;
} else {
return false;
}
} else {
return false;
}
}
function clear_buffer() {
$this->buffer = '';
unset($this->buffer);
return true;
}
}#end of class
|