All pastes #637098 Raw Edit

Something

public text v1 · immutable
#637098 ·published 2007-07-28 15:12 UTC
rendered paste body
<?php

set_time_limit(0);

class Server
{
	var $sock;
	var $maxclients = 2;
	var $client = array();
	
	function Server($address, $port)
	{
		$this->sock = socket_create(AF_INET, SOCK_STREAM, 0);
		socket_bind($this->sock, $address, $port) or die('Could not bind to address/port');
		$this->log("Listening on port ". $port);
		socket_listen($this->sock);
		
		while(true)
		{
			$read[0] = $this->sock;
			for($i = 0; $i < $this->maxclients; $i++)
			{
				if(@$this->client[$i]['sock'] != null)
				{
					$read[$i + 1] = $client[$i]['sock'];
				}
			}
			
			$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
			
			if(in_array($this->sock, $read))
			{
				for($i = 0; $i < $this->maxclients; $i++)
				{
					if($client[$i]['sock'] == null)
					{
						$client[$i]['sock'] = socket_accept($this->sock);
						break;
					}
					elseif($i == $this->maxclients - 1)
					{
						$this->log("Too many clients connected");
					}
				}
				if (--$ready <= 0)
				{
					continue;
				}
			}
		}
		
		socket_close($this->sock);
	}
	
	function log($message)
	{
		echo $message . "\n";
	}
}

$server = new Server("127.0.0.1", 43594);

?>