host = $host; $this -> port = $port; $this -> buffer = $buffer; $this -> endl = $endl; $this -> timeout = $timeout; $this -> recvtimeout = $recvtimeout; $this -> connection = fsockopen($this -> host, $this -> port, $this -> errno, $this -> errstr, $this -> timeout); if(!$this -> connection){ $this -> error("Could not connect to ".$this -> host.":".$this -> port."!"); } if($this -> recvtimeout != false) stream_set_timeout($this -> connection, $this -> recvtimeout); } public function checkConnection(){ if(!is_resource($this -> connection)){ $this -> close(); return false; } else{ return true; } } public function isConnected(){ return ($this -> connection && is_resource($this -> connection)) ? true : false; } public function gotData(){ return ($this -> isConnected()) ? feof($this -> connection) : false; } public function send($msg){ $this -> checkConnection(); if(trim($msg) != ""){ if(!fwrite($this -> connection, $msg.$this -> endl)){ if($this -> debug) echo "[Fsock] Could not send:'".$msg."'"; return false; } else{ if($this -> debug) echo "[Fsock] Sent: ".$msg."\n"; return true; } } } public function recv(){ $this -> checkConnection(); if(!feof($this -> connection)){ if(($recv = fread($this -> connection, $this -> buffer)) != ''){ if($this -> debug) echo "[Fsock] Recv: ".$recv."\n"; return $recv; } else{ return false; } } else{ return false; } } public function close(){ if($this -> connection){ fclose($this -> connection); unset($this -> connection); } } function __destruct(){ $this -> close(); } public function error($msg){ echo "[ClientFSockClass] ".$msg."\n"; //die(); } } ?>