Socket.class.inc
phudbase/server/includes/classes/Server/Socket.class.inc · 1.2 KB
From the archive phudbase-1.11.tar.gz
Download the archive « Back to the archive contents
<?php
class Socket
{
private $socket;
private $address;
private $port;
private $blocking;
function __construct($address = "localhost", $port=12345, $blocking=true)
{
$this->address = $address;
$this->port = $port;
$this->blocking = $blocking;
}
function open()
{
// Create a TCP Stream socket
$this->socket = socket_create(AF_INET, SOCK_STREAM, 0);
// Set the REUSE option before binding //
if(!socket_set_option($this->socket,SOL_SOCKET,SO_REUSEADDR,1))
{
print(socket_strerror(socket_last_error($this->socket)));
die();
}
$time = time();
$bound = false;
// Bind the socket to an address/port
while ($time + 10 > time() && !$bound)
{
print ("Attempting to bind to port {$this->port}...\n");
$bound = socket_bind($this->socket, $this->address, $this->port);
if ($bound)
break;
else
sleep(1);
}
if (!$bound)
{
print ("Could not bind...\n");
die();
}
}
function close()
{
}
function getSocket()
{
return $this->socket;
}
function getAddress()
{
return $this->address;
}
function getPort()
{
return $this->port;
}
function getBlocking()
{
return $this->blocking;
}
}
?>