00001 <?php
00030 class zSocketClient extends zCore{
00031 var $host;
00032 var $port;
00033 var $timeout;
00034 var $sep;
00035 var $connected;
00036 var $map;
00037 var $protocol;
00038 var $handle;
00039 var $cnv;
00040 var $conversation;
00041
00042 function zSocketClient($host=null, $port=null, $timeout=null){
00043 $this->__construct($host, $port, $timeout);
00044 }
00045 function __construct($host=null, $port=null, $timeout=null){
00046 $this->host = $host;
00047 $this->port = $port;
00048 $this->timeout = $timeout;
00049 $this->sep = "\r\n";
00050 $this->protocol = 'tcp';
00051 $this->conversation = new stdClass();
00052 $this->conversation->client = array();
00053 $this->conversation->server = array();
00054 }
00060 function setHost($hostName){
00061 $this->host = $hostName;
00062 }
00067 function getHost(){
00068 return $this->host;
00069 }
00075 function setPort($port){
00076 $this->port = $port;
00077 }
00082 function getPort(){
00083 return $this->port;
00084 }
00090 function setTimeOut($timeOut){
00091 $this->timeout = $timeOut;
00092 }
00098 function getTimeOut(){
00099 return $this->timeout;
00100 }
00106 function setSeparator($sep){
00107 $this->sep = $sep;
00108 }
00114 function useUDP($flag = true){
00115 $this->protocol = $flag ? 'udp' : 'tcp';
00116 }
00122 function isUDP(){
00123 return ($this->protocol == 'udp');
00124 }
00130 function useTCP($flag=true){
00131 $this->protocol = $flag ? 'tcp' : $this->protocol;
00132 }
00138 function isTCP(){
00139 return ($this->protocol == 'tcp');
00140 }
00145 function useSSL($flag = true){
00146 $this->protocol = $flag ? 'ssl' : '';
00147 }
00153 function isSSL(){
00154 return ($this->protocol == 'ssl');
00155 }
00161 function useTLS($flag=true){
00162 $this->protocol = $flag ? 'tls' : '';
00163 }
00169 function isTLS(){
00170 return ($this->protocol == 'tls');
00171 }
00176 function connect(){
00177 if(!$this->isConnectable()){
00178 perror("<code>zigmoyd.socket.connect</code><br />Sorry Server not Connectable");
00179 exit;
00180 }
00181 if(!$this->isConnected()){
00182 if(!$this->handle = @pfsockopen($this->host, $this->port, $errNo, $errStr, $this->timeout)){
00183 perror("<code>zigmoyd.socket.connect</code><br />Failed to Connect with $this->host on Port $this->port<br />Error No: $errNo<br />Error Text: $errStr");
00184 if(class_exists('zLogger'))zLogger::error("Failed to Connect with Host $this->host on Port $this->port Error: ($errNo) $errStr", 'socket.connect');
00185 $this->connected = false;
00186 }else{
00187 if(class_exists('zLogger'))zLogger::debug("Successfully Connected to Host $this->host on Port $this->port<br />Error No: $errNo<br />Error Text: $errStr", 'socket.connect');
00188 $this->connected = true;
00189 }
00190 }
00191 return $this->connected;
00192 }
00198 function isConnected(){
00199 return $this->connected;
00200 }
00206 function isConnectable(){
00207 if(strlen($this->protocol) <= 1){
00208 perror('<code>zigmoyd.socket.isConnectable</code><br />Invalid Protocol');
00209 if(class_exists('zLogger'))zLogger::error('Invalid Protocol', 'zigmoyd.socket');
00210 return false;
00211 }
00212 if(strlen($this->host) <= 1){
00213 perror('<code>zigmoyd.socket.isConnectable</code><br />Invalid Host');
00214 if(class_exists('zLogger'))zLogger::error('Invalid Host', 'zigmoyd.socket');
00215 return false;
00216 }
00217 if(strlen($this->port) <= 1){
00218 perror('<code>zigmoyd.socket.isConnectable</code><br />Invalid Port');
00219 if(class_exists('zLogger'))zLogger::error('Invalid Port', 'zigmoyd.socket');
00220 return false;
00221 }
00222 if(!$this->timeout){
00223 $this->timeout = 30;
00224 }
00225 return true;
00226 }
00233 function send($data){
00234 $this->conversation->client[] = $data;
00235 fflush($this->handle);
00236 $ret = fwrite($this->handle, $data.$this->sep);
00237 if(!$ret)perror('<code>zigmoyd.socket.send</code><br />Failed to Write on Sicket');
00238 return $ret;
00239 }
00245 function receive($maxLen=0){
00246 $return = '';
00247 while(is_string(($ch = fgetc($this->handle)))){
00248 if($maxLen != 1){
00249 $return .= $ch;
00250 --$maxLen;
00251 }
00252 }
00253 $this->conversation->server[] = $return;
00254 return $return;
00255 }
00260 function disconnect(){
00261 if(is_resource($this->handle)){
00262 if(class_exists('zLogger'))zLogger::debug("Closing Socket Connection as not yet Closed", "socket.disconnect");
00263 fclose($this->handle);
00264 }else{
00265 if(class_exists('zLogger'))zLogger::debug("NOT Closing Socket Connection as already Closed", "socket.disconnect");
00266 }
00267 }
00272 function __destruct(){
00273 $this->disconnect();
00274 }
00275 }
00277 ?>
00278 <?php
00279
00280
00281
00282
00283 ?>