I started building a similar server some time ago but never really finished it. It works and accepts mutliple connections (without threading or forking processes) simultaneously but doesn't do anything besides display their input. I'll post the code below...
<?
//set the initial variables
$host = "xxx.xxx.xxx.xxx";
$port = xxxx;
//disable timout
set_time_limit(0);
//create the socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
//make it re-usable
socket_setopt($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_nonblock($socket);
//bind it to the specified port
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
//set it to listen for connections
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
//allocate the socket list
$sockList = socket_fd_alloc($sockList);
//initialize the socket list
socket_fd_zero($sockList);
//add our main socket to the list
socket_fd_set($sockList,$socket);
//set the client count to 0(no clients yet)
$clientCount = 0;
//terminator for our while loop
$abort = 0;
//Send some output to the console
print "Chat Server Initialized\n";
print "Waiting for Connections\n";
//loop until we say so
while($abort == 0) {
//zero the list again since it will be modified later in the loop
socket_fd_zero($sockList);
//add the main socket to the list
socket_fd_set($sockList,$socket);
//add all the clients to the list
for($x=0; $x<=$clientCount;$x++) {
if($client[$x] != NULL) {
socket_fd_set($sockList,$client[$x]);
}
}
//wait for something to happen
$set = socket_select($sockList, $write = NULL, $except = NULL, 0);
if($set > 0) {
//if the main socket changed, its a new connection
if(socket_fd_isset($sockList, $socket)) {
//accept the connection
$spawn = socket_accept($socket);
//set the address to be re-usable
socket_setopt($spawn, SOL_SOCKET, SO_REUSEADDR, 1);
if($spawn > 0) {
//We've added a client, so lets increment the counter
$clientCount++;
//Loop through the client list until we find a blank space for the new client
for($x=0;$x<=$clientCount;$x++) {
if($client[$x] == NULL) {
$client[$x] = $spawn;
//Print a welcome message
$msg = "Welcome\n";
socket_write($client[$x],$msg,strlen($msg));
//Send a message to the console informing us of the new connection
print "New connection accepted\n";
break;
}
}
}
} else {
//If it wasn't the main socket, one of our clients said something
//Loop through and see who it was
for($x=0;$x<=$clientCount;$x++) {
if(socket_fd_isset($sockList,$client[$x])) {
//Read their input
$read = trim(socket_read($client[$x],1024));
if($read == NULL) {
socket_close($client[$x]);
$client[$x] = NULL;
print "Client #$x closed the connection\n";
break;
} else if($read == "?") {
$msg = "Host: xxx.xxx.xxx.xxx\n";
socket_write($client[$x],$msg,strlen($msg));
echo "Sent out put $msg";
break;
}
//Print it to the screen
echo "Client #$x: " . $read . "\n";
//break the loop and start again.
break;
}
}
}
}
}
// close sockets
socket_close($socket);
?>
Like I said, it's not very useful, but it will show you how to accept multiple connections at once