You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.0 KiB

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