0

我正在尝试在 XAMPP 中创建具有多个客户端的动态 SQL 表。我已经运行了服务器代码来处理多个客户端。下面是我的多客户端代码,它通过不断监听传入客户端并根据需要打开套接字连接来完全工作。

问题:建立连接后,我需要为每个客户端创建一个单独的 SQL 数据库。

前任。我有一个值的 SQL 表:温度值时间

客户端 1 发送消息:84 摄氏度 11:43:01

假设另一个客户随机加入

客户端 2 发送消息:88 Farenheit 11:43:01

除非有更好的方法,否则我这样做是为了有一个带有下拉菜单的状态页面,其中包含每个客户端的选择,并且由于明显的冲突,数据不能存储在同一个表中。如果没有,有人能指出我正确的方向吗?

谢谢!

<?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;
}
}
}
}
?>
4

0 回答 0