Skip to content

Commit 7fc6b06

Browse files
committed
Socket Programming Class and example file
1 parent 343cd0c commit 7fc6b06

File tree

3 files changed

+444
-0
lines changed

3 files changed

+444
-0
lines changed

PrinceSocket.Class

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
defined('PRINCE_SOCKET')or die('no direct connect');
3+
4+
if( !class_exists( 'PrinceSocket' ) )
5+
{
6+
private static var $_singleton = null;
7+
private var $_hostname;
8+
private var $_port;
9+
private var $_link;
10+
private var $_error_no;
11+
private var $_error_str;
12+
private var $_persist = false;
13+
private var $_timeout = 30;
14+
15+
private static function __construct()
16+
{
17+
18+
}
19+
20+
public static function Singleton()
21+
{
22+
if( self::$_singleton == null || $_singleton != instanceof PrinceSocket )
23+
{
24+
self::$_singleton = new PrinceSocket();
25+
}
26+
27+
return self::$_singleton;
28+
}
29+
30+
public function _Connect( $host, $port )
31+
{
32+
$this->_hostname = ( !empty( $host ) ? $host : '' );
33+
$this->_port = ( !empty( $port ) ? $port : '' );
34+
$pconnect = ( !empty( $this->_persist ) ? "pfsockopen" : "fsockopen" );
35+
$this->_timeout = 60;
36+
37+
if( $this->_hostname && $this->_port )
38+
{
39+
$this->_link = new $pconnect( $this->_hostname, $this->_port, $this->_error_no, $this->_error_str, $this->_timeout );
40+
41+
stream_set_blocking( $this->_link, 0 );
42+
return $this->_link;
43+
}
44+
else {
45+
return $this->PrinceError( 'Error occurred' );
46+
}
47+
}
48+
49+
private function PrinceError( $errorMsg )
50+
{
51+
if( !empty( $errorMsg ) )
52+
{
53+
throw new Exception( $errorMsg );
54+
}
55+
else{
56+
//
57+
}
58+
return false;
59+
}
60+
61+
private function PrinceCMD( $cmd )
62+
{
63+
if( $this->_link != false )
64+
{
65+
$cmd .= "\r\n";
66+
$result = fwrite( $this->_link, $cmd, strlen( $cmd ) );
67+
68+
if( $result != '' )
69+
{
70+
return $result;
71+
}
72+
}
73+
else {
74+
$this->PrinceError('NO LINK ERROR');
75+
}
76+
}
77+
78+
public function getResponse()
79+
{
80+
if( $this->_link )
81+
{
82+
$response = fread( $this->_link, 2048 );
83+
84+
if( $response != '' )
85+
{
86+
return $response;
87+
}
88+
}
89+
else{
90+
$this->PrinceError( 'NO LINK' );
91+
}
92+
}
93+
94+
public function _readLine()
95+
{
96+
$line = '';
97+
if( $this->_line )
98+
{
99+
$line = fgets( $this->_line, 1024 );
100+
101+
if( strlen( $line >= 2 && substr( $line, -2 ) == "\r\n" || substr( $line, -1 ) == "\n" ) )
102+
{
103+
return rtrim( $line );
104+
}
105+
106+
return $line;
107+
}
108+
}
109+
110+
public function getMultilinedResponse()
111+
{
112+
$data = '';
113+
while(($tmp = $this->_readLine()) != '.')
114+
{
115+
if(substr($tmp, 0, 2) == '..')
116+
{
117+
$tmp = substr($tmp, 1);
118+
}
119+
$data .= $tmp."\r\n";
120+
}
121+
122+
return substr($data, 0, -2);
123+
}
124+
125+
126+
127+
}// end PrinceSocket
128+
129+
130+
131+
132+
?>

socket.class

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<?php
2+
3+
defined('CONNECTED') ? CONNECTED : define("CONNECTED", true);
4+
defined('DISCONNECTED')? DISCONNECTED : define("DISCONNECTED", false);
5+
6+
/**
7+
* Socket class
8+
*
9+
* This class can be used to connect to external sockets and communicate with the server
10+
*
11+
* @author Tom Reitsma <treitsma@rse.nl>
12+
* @version 0.6
13+
*/
14+
15+
if(!class_exists('Socket')){
16+
17+
Class Socket
18+
{
19+
/**
20+
* @var singleton $instance
21+
* @desc Singleton var
22+
*/
23+
private static $instance;
24+
25+
/**
26+
* @var resource $connection
27+
* @desc Connection resource
28+
*/
29+
private $connection = null;
30+
31+
/**
32+
* @var string $connectionState
33+
* @desc
34+
*/
35+
private $connectionState = DISCONNECTED;
36+
37+
/**
38+
* @var int $defaultHost
39+
* @desc Default ip address to connect to
40+
*/
41+
private $defaultHost = "192.211.62.21";
42+
43+
/**
44+
* @var int $defaultPort
45+
* @desc Default port to connect to
46+
*/
47+
private $defaultPort = 110;
48+
49+
/**
50+
* @var float $defaultTimeout
51+
* @desc Default timeout for connection to a server
52+
*/
53+
private $defaultTimeout = 10;
54+
55+
/**
56+
* @var bool $persistentConnection
57+
* @desc Determines wether to use a persistent socket connection or not
58+
*/
59+
private $persistentConnection = false;
60+
61+
/**
62+
* Class constructor
63+
*
64+
* @return void
65+
* @access private
66+
*/
67+
private function __construct()
68+
{
69+
70+
}
71+
72+
/**
73+
* Singleton pattern. Returns the same instance to all callers
74+
*
75+
* @return Socket
76+
*/
77+
public static function singleton()
78+
{
79+
if (self::$instance == null || ! self::$instance instanceof Socket)
80+
{
81+
self::$instance = new Socket();
82+
}
83+
return self::$instance;
84+
}
85+
86+
/**
87+
* Connects to the socket with the given address and port
88+
*
89+
* @return void
90+
*/
91+
public function connect($serverHost=false, $serverPort=false, $timeOut=false)
92+
{
93+
$socketFunction = $this->persistentConnection ? "fsockopen" : "pfsockopen";
94+
95+
// Check if the function parameters are set.
96+
// If not, use the class defaults
97+
if($serverHost == false)
98+
{
99+
$serverHost = $this->defaultHost;
100+
}
101+
102+
if($serverPort == false)
103+
{
104+
$serverPort = $this->defaultPort;
105+
}
106+
107+
if($timeOut == false)
108+
{
109+
$timeOut = $this->defaultTimeout;
110+
}
111+
112+
$connection = $socketFunction($serverHost, $serverPort, $errorNumber, $errorString, $timeOut);
113+
$this->connection = $connection;
114+
115+
stream_set_blocking($this->connection, 0);
116+
117+
if($connection == false)
118+
{
119+
$this->_throwError("Connecting to {$serverHost}:{$serverPort} failed.<br>Reason: {$errorString}");
120+
}
121+
122+
$this->connectionState = CONNECTED;
123+
}
124+
125+
/**
126+
* Disconnects from the server
127+
*
128+
* @return True on succes, false if the connection was already closed
129+
*/
130+
public function disconnect()
131+
{
132+
if($this->validateConnection())
133+
{
134+
fclose($this->connection);
135+
$this->connectionState = DISCONNECTED;
136+
137+
return true;
138+
}
139+
140+
return false;
141+
}
142+
143+
/**
144+
* Sends a command to the server
145+
*
146+
* @return string Server response
147+
*/
148+
public function sendCmd($command)
149+
{
150+
if($this->validateConnection())
151+
{
152+
$command .= "\r\n";
153+
154+
$result = fwrite($this->connection, $command, strlen($command));
155+
156+
return $result;
157+
}
158+
159+
$this->_throwError("Sending command \"{$command}\" failed.<br>Reason: Not connected");
160+
}
161+
162+
/**
163+
* Gets the server response (not multilined)
164+
*
165+
* @return string Server response
166+
*/
167+
public function getResponse()
168+
{
169+
if($this->validateConnection())
170+
{
171+
return fread($this->connection, 2048);
172+
}
173+
174+
$this->_throwError("Receiving response from server failed.<br>Reason: Not connected");
175+
}
176+
177+
/**
178+
* Gets a multilined response
179+
*
180+
* @return string Server response
181+
*/
182+
public function getMultilinedResponse()
183+
{
184+
$data = '';
185+
while(($tmp = $this->readLine()) != '.')
186+
{
187+
if(substr($tmp, 0, 2) == '..')
188+
{
189+
$tmp = substr($tmp, 1);
190+
}
191+
$data .= $tmp."\r\n";
192+
}
193+
194+
return substr($data, 0, -2);
195+
}
196+
197+
/**
198+
* Reads an entire line
199+
*
200+
* @return string Server response
201+
*/
202+
public function readLine()
203+
{
204+
$line = '';
205+
while (!feof($this->connection))
206+
{
207+
$line .= fgets($this->connection, 1024);
208+
if (strlen($line) >= 2 && (substr($line, -2) == "\r\n" || substr($line, -1) == "\n"))
209+
{
210+
return rtrim($line);
211+
}
212+
}
213+
return $line;
214+
}
215+
216+
/**
217+
* Validates the connection state
218+
*
219+
* @return bool
220+
*/
221+
private function validateConnection()
222+
{
223+
return (is_resource($this->connection) && ($this->connectionState != DISCONNECTED));
224+
}
225+
226+
/**
227+
* Throws an error
228+
*
229+
* @return void
230+
*/
231+
private function _throwError($errorMessage)
232+
{
233+
throw new Exception("Socket error: " . $errorMessage);
234+
}
235+
236+
/**
237+
* If there still was a connection alive, disconnect it
238+
*/
239+
public function __destruct()
240+
{
241+
$this->disconnect();
242+
}
243+
}
244+
}
245+
246+
?>

0 commit comments

Comments
 (0)