host = $host; $this->port = $port; $this->connected = false; } /** * Connects to host with specified settings, accepts connection timeout (optional, default 30) * @method connect * @param optional int connectionTimeout * @returns true if successful, false otherwise */ function connect($connectTimeout = 30) { $this->connectionID = fsockopen($this->host, $this->port, $errorID, $errorDesc, $connectTimeout); if($this->connectionID === false) { return false; } else { $this->connected = true; return true; } } /** * Disconnects if already connected * @method disconnect * @returns true if successful, false otherwise */ function disconnect() { $success = fclose($this->connectionID); if($success) $this->connected = false; return $success; } /** * Receives data through connected socket, accepts chunk size (optional, default 4096) * @method receive * @param optional int chunkSize * @returns string received data if successful, false otherwise */ function receive($chunkSize = 4096) { $receivedString = ""; $success = false; if($this->connected) { while(!feof($this->connectionID)) { $receivedString .= fgets($this->connectionID, $chunkSize); } $success = true; } if($success) return $receivedString; else return false; } /** * Sends data through connected socket * @method send * @param string sendString * @returns true if successful, false otherwise */ function send($sendString) { $success = false; if($this->connected) $success = fwrite($this->connectionID, $sendString); return $success; } /** * Combination of send and receive methods in one * @method sendReceive * @param sendString * @param optional int connectionTimeout * @returns string received data if successful, false otherwise */ function sendReceive($sendString, $receiveChunkSize = 4096) { $success = true; $receivedString = ""; if($this->connected) { $bytesSent = $this->send($sendString); if($bytesSent === false) $success = false; if($success) { $receivedString = $this->receive($receiveChunkSize); if($receivedString === false) $success = false; } } if($success) return $receivedString; else return false; } /** * Sets host to make a connection to * @method setHost * @param string host * @returns none */ function setHost($host) { $this->host = $host; } /** * Sets port to use for the connection * @method setPort * @param int port * @returns none */ function setPort($port) { $this->port = $port; } }