<?PHP
class Socket {
public $socket = NULL;
public $type = NULL;
public $family = NULL;
public $protocol = NULL;
public $connected = false;
function Socket($f = AF_INET, $t = SOCK_STREAM, $p = SOL_TCP) {
$this->family = $f;
$this->type = $t;
$this->protocol = $p;
$this->socket = socket_create($f, $t, $p);
}
function connect($host, $port) {
if(!socket_connect($this->socket, $host, $port)) {
trigger_error("Error: " . socket_strerror(socket_last_error($this->socket)));
socket_clear_error($this->socket);
$this->connected = false;
return false;
}
else {
$this->connected = true;
return true;
}
}
function disconnect() {
if(!socket_close($this->socket)) {
trigger_error("Error: Could not disconnect socket");
}
$this->connected = false;
$this->socket = socket_create($this->family, $this->type, $this->protocol);
}
function sendData($data) {
if(!@socket_write($this->socket, $data, strlen($data))) {
$this->connected = false;
return false;
}
return true;
}
function recvData($len = 4096) {
//$select = array($this->socket);
//socket_select($select, $write = NULL, $exept = NULL, 0);
$read = '';
$readlen = @socket_recv($this->socket, $read, $len, MSG_WAITALL);
if($readlen === false || $readlen === '' || $readlen < $len) {
$this->connected = false;
return false;
}
else {
return $read;
}
}
function hasData() {
$readable = NULL;
$read = array($this->socket);
$readable = socket_select($read, $write = NULL, $exception = NULL, 0);
return ($readable != 0);
}
}
?>