I'm trying to create dynamic SQL tables with multiple clients in XAMPP. I already have the server code running to handle multiple clients. Below is my multiple client code, which fully works by constantly listening for incoming clients and opens a socket connection as needed.
The problem: I need to create a separate SQL database for each client after it has made a connection.
Ex. I have a SQL table for values: Temperature Value Time
Client 1 sends message: 84 Celcius 11:43:01
assuming another client joins at a random time
Client 2 sends message: 88 Farenheit 11:43:01
Unless there is a better way, I'm doing this so I can have a status page with a drop down menu with selections for each client and data can't be stored in the same table due to obvious conflicts. If not could someone point me in the right direction?
Thanks!
<?php
error_reporting(1);
ini_set(‘display_errors’, ’1');
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$max_clients = 10;
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($socket, 0, 10000);
socket_listen($socket, $max_clients);
$clients = array(’0' => array(‘socket’ => $socket));
while(TRUE)
{
$read[0] = $socket;
for($i=1; $i<count($clients)+1; ++$i)
{
if($clients[$i] != NULL)
{
$read[$i+1] = $clients[$i]['socket'];
}
}
$ready = socket_select($read, $write = NULL, $except = NULL, $tv_sec = NULL);
if(in_array($socket, $read))
{
for($i=1; $i < $max_clients+1; ++$i)
{
if(!isset($clients[$i]))
{
$clients[$i]['socket'] = socket_accept($socket);
socket_getpeername($clients[$i]['socket'],$ip);
$clients[$i]['ipaddy'] = $ip;
socket_write($clients[$i]['socket'], ‘Welcome to my Custom Socket Server’."\r\n");
socket_write($clients[$i]['socket'], ‘There are ‘.(count($clients) – 1).’ client(s) connected to this server.’."\r\n");
echo ‘New client connected: ‘ . $clients[$i]['ipaddy'] .’ ‘;
break;
}
elseif($i == $max_clients – 1)
{
echo ‘To many Clients connected!’."\r\n";
}
if($ready < 1)
{
continue;
}
}
}
for($i=1; $i<$max_clients+1; ++$i)
{
if(in_array($clients[$i]['socket'], $read))
{
$data = @socket_read($clients[$i]['socket'], 1024, PHP_NORMAL_READ);
if($data === FALSE)
{
unset($clients[$i]);
echo ‘Client disconnected!’,"\r\n";
continue;
}
$data = trim($data);
if(!empty($data))
{
if($data == ‘exit’)
{
socket_write($clients[$i]['socket'], ‘Thanks for trying my Custom Socket Server, goodbye.’."\n");
echo ‘Client ‘,$i,’ is exiting.’,"\n";
unset($clients[$i]);
continue;
}
for($j=1; $j<$max_clients+1; ++$j)
{
if(isset($clients[$j]['socket']))
{
if(($clients[$j]['socket'] != $clients[$i]['socket']) && ($clients[$j]['socket'] != $socket))
{
echo($clients[$i]['ipaddy'] . ‘ is sending a message!’."\r\n");
socket_write($clients[$j]['socket'], ‘[' . $clients[$i]['ipaddy'] . ‘] says: ‘ . $data . "\r\n");
}
}
}
break;
}
}
}
}
?>