1<?php 2 3/* 4 This file is part of ActiveLink PHP NET Package (www.active-link.com). 5 Copyright (c) 2002-2004 by Zurab Davitiani 6 7 You can contact the author of this software via E-mail at 8 hattrick@mailcan.com 9 10 ActiveLink PHP NET Package is free software; you can redistribute it and/or modify 11 it under the terms of the GNU Lesser General Public License as published by 12 the Free Software Foundation; either version 2.1 of the License, or 13 (at your option) any later version. 14 15 ActiveLink PHP NET Package is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU Lesser General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public License 21 along with ActiveLink PHP NET Package; if not, write to the Free Software 22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 23*/ 24 25/** 26 * Socket class provides a basic network socket functionality 27 * @class Socket 28 * @package org.active-link.net 29 * @author Zurab Davitiani 30 * @version 0.4.0 31 */ 32 33class Socket { 34 35 // protected properties 36 var $host; 37 var $port; 38 var $connected; 39 var $connectionID; 40 41 /** 42 * Constructor, accepts host and port, initializes object 43 * @method Socket 44 * @param host 45 * @param port 46 */ 47 function Socket($host, $port) { 48 $this->host = $host; 49 $this->port = $port; 50 $this->connected = false; 51 } 52 53 /** 54 * Connects to host with specified settings, accepts connection timeout (optional, default 30) 55 * @method connect 56 * @param optional int connectionTimeout 57 * @returns true if successful, false otherwise 58 */ 59 function connect($connectTimeout = 30) { 60 $this->connectionID = fsockopen($this->host, $this->port, $errorID, $errorDesc, $connectTimeout); 61 if($this->connectionID === false) { 62 return false; 63 } 64 else { 65 $this->connected = true; 66 return true; 67 } 68 } 69 70 /** 71 * Disconnects if already connected 72 * @method disconnect 73 * @returns true if successful, false otherwise 74 */ 75 function disconnect() { 76 $success = fclose($this->connectionID); 77 if($success) 78 $this->connected = false; 79 return $success; 80 } 81 82 /** 83 * Receives data through connected socket, accepts chunk size (optional, default 4096) 84 * @method receive 85 * @param optional int chunkSize 86 * @returns string received data if successful, false otherwise 87 */ 88 function receive($chunkSize = 4096) { 89 $receivedString = ""; 90 $success = false; 91 if($this->connected) { 92 while(!feof($this->connectionID)) { 93 $receivedString .= fgets($this->connectionID, $chunkSize); 94 } 95 $success = true; 96 } 97 if($success) 98 return $receivedString; 99 else 100 return false; 101 } 102 103 /** 104 * Sends data through connected socket 105 * @method send 106 * @param string sendString 107 * @returns true if successful, false otherwise 108 */ 109 function send($sendString) { 110 $success = false; 111 if($this->connected) 112 $success = fwrite($this->connectionID, $sendString); 113 return $success; 114 } 115 116 /** 117 * Combination of send and receive methods in one 118 * @method sendReceive 119 * @param sendString 120 * @param optional int connectionTimeout 121 * @returns string received data if successful, false otherwise 122 */ 123 function sendReceive($sendString, $receiveChunkSize = 4096) { 124 $success = true; 125 $receivedString = ""; 126 if($this->connected) { 127 $bytesSent = $this->send($sendString); 128 if($bytesSent === false) 129 $success = false; 130 if($success) { 131 $receivedString = $this->receive($receiveChunkSize); 132 if($receivedString === false) 133 $success = false; 134 } 135 } 136 if($success) 137 return $receivedString; 138 else 139 return false; 140 } 141 142 /** 143 * Sets host to make a connection to 144 * @method setHost 145 * @param string host 146 * @returns none 147 */ 148 function setHost($host) { 149 $this->host = $host; 150 } 151 152 /** 153 * Sets port to use for the connection 154 * @method setPort 155 * @param int port 156 * @returns none 157 */ 158 function setPort($port) { 159 $this->port = $port; 160 } 161 162} 163