phool4fool
203.***.***.***Socket Abstraction Layer v7.0
There are comments after each function in the class which describe their functionality and use. Scroll down to below the class to see the example use PHP code.Socket Abstraction Layer v7.0 class
PHP code:
<?php
/*
* $Id: sockets.php,v 7.00 2003/05/07 4:45:00 Daeken Exp $
*
* Copyright (C) 2003 by Cody Brocious (daeken_9999 AT yahoo DOT com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
define('FURTHER_READ', 0);
define('FURTHER_SEND', 1);
define('FURTHER_ALL', 2);
class Socket
{
var $Socket;
var $proto;
var $end;
var $connected;
var $Queue;
function Socket($proto = SOL_TCP) // Creates the socket. This will be called when you create a Socket object.
{
if($proto == SOL_TCP)
$type = SOCK_STREAM;
elseif($proto == SOL_UDP)
$type = SOCK_DGRAM;
else
die(''' . $proto . '' is not a valid protocol type.' . "n");
$this->Socket = socket_create(AF_INET, $type, $proto);
$this->proto = $proto;
if (strtoupper(substr(PHP_OS, 0,3) == 'WIN'))
$this->end = "rn";
else
$this->end = "n";
$this->Queue = '';
}
function Connect($host, $port) // Connect to the host on the port given.
{
$res = socket_connect($this->Socket, $host, $port) or die('Cannot connect to ' . $host . ':' . $port . '. Reason: ' . socket_strerror(socket_last_error($this->Socket)) . $this->end);
$this->connected = true;
return $res;
}
function Write($string = "x0", $suppress = true) // Write to a connected socket.
{
$res = @socket_write($this->Socket, $string) or die((!$suppress) ? $this->Error() : null);
return $res;
}
function Send($string, $host, $port = null) // Send a packet. Use this if you are using UDP.
{
socket_sendto($this->Socket, $string, strlen($string), 0, gethostbyname($host), $port) or die($this->Error());
}
function Recv($len) // Recieve from a socket. Use this if you are using UDP.
{
$buf = null;
$host = null;
$port = null;
socket_recvfrom($this->socket, $buf, $len, 0, $host, $port) or die($this->Error());
return array($buf, $host, $port);
}
function Read_Binary($length) // Reads $length number of bytes without stopping at n or r.
{
if(strlen($this->Queue) > 0)
{
if($length >= strlen($this->Queue))
{
$str = $this->Queue;
$this->Queue = '';
}
else
{
$str = substr($this->Queue, 0, $length);
$this->Queue = substr($this->Queue, $length);
}
$length -= strlen($str);
}
else
$str = '';
$str .= socket_read($this->Socket, $length, PHP_BINARY_READ) or die($this->Error());
return $str;
}
function Read($length, $suppress = true) // Reads $length number of bytes, stopping at r or n.
{
if(strlen($this->Queue) > 0)
{
if($length >= strlen($this->Queue))
{
$str = $this->Queue;
$this->Queue = '';
}
else
{
$str = substr($this->Queue, 0, $length);
$this->Queue = substr($this->Queue, $length);
}
$length -= strlen($str);
}
else
$str = '';
if(strstr(strtolower(PHP_OS), 'win'))
{
$i = 0;
while($i++ < $length)
{
$char = @socket_read($this->Socket, 1, PHP_BINARY_READ) or die((!$suppress) ? $this->Error() : null);
if($char == "r" || $char == "n")
break;
$str .= $char;
}
}
else
$str .= @socket_read($this->Socket, $length, PHP_NORMAL_READ) or die((!$suppress) ? $this->Error() : null);
return $str;
}
function Data_Queued() // Checks if the other side of the connection has sent new data.
{
$arr = array($this->Socket);
if(socket_select($arr, $t = null, $te = null, 1) > 0)
return true;
else
return false;
}
function Blocking($block = true) // Sets blocking mode.
{
if($block === false)
return socket_set_nonblock($this->Socket) or die($this->Error());
else
return socket_set_block($this->Socket) or die($this->Error());
}
function Error() // For internal use only. Sends the last socket error and shuts down the script.
{
echo 'Error in processing your request: ', socket_strerror(socket_last_error($this->Socket)), $this->end;
exit();
}
function Shutdown($how = FURTHER_ALL) // Turns off reading, writing, or both on the socket. Expects FURTHER_ALL, FURTHER_READ, or FURTHER_WRITE.
{
socket_shutdown($this->Socket, $how) or die($this->Error());
}
function Close() // Close the socket.
{
socket_close($this->Socket) or die($this->Error());
$this->connected = false;
}
function Queue($str) // Post data into the data to read; it will be read before the data send from the other side of the connection.
{
$this->Queue .= $str;
}
function Listen($port, $callback = null) // Listen on port specified by the first parameter, which will call the callback in the optional second parameter if there is a connection. If you don't specify a callback, the listening socket will be returned.
{
socket_set_option($this->Socket, SOL_SOCKET, SO_REUSEADDR, 1);
$res = socket_bind($this->Socket, 0, $port) or die($this->Error());
$res2 = socket_listen($this->Socket, 15) or die($this->Error());
if($this->proto == SOL_UDP || $callback === null)
return array($res, $res2);
while($conn = socket_accept($this->Socket))
{
if(function_exists('pcntl_fork'))
{
$pid = pcntl_fork();
if($pid == -1)
die('Couldn\'t Fork.' . $this->end);
elseif($pid == 0)
{
$callback($conn);
exit;
}
else
socket_close($conn);
}
else
{
$sock = new Socket();
$sock->socket = $conn;
call_user_func($callback, $sock);
socket_close($conn);
}
}
}
}
?>
To use this class to connect to a server via TCP on port 5000, you could do it this way:
PHP code:
<?php
$sock = new Socket();
$sock->connect('server.com', '5000');
$sock->Write('Hi!'); // Write Hi! to the socket.
$str = $sock->Read(50); // This reads to 50 characters or to a newline.
?>
To listen for connections, you might do this:
PHP code:
<?php
function YourFunc($sock)
{
$connection = new Socket;
$connection->Socket = $sock;
$connection->Write('Hi!');
}
$sock = new Socket();
$sock->Listen('5000', YourFunc);
?>