【问题标题】:client and server php socket客户端和服务器 php 套接字
【发布时间】:2018-08-02 13:10:31
【问题描述】:

我使用 PHP 进行地理定位项目,我使用套接字我遇到了问题,我解释一下: 客户端(框 GPS)发送其标识符(IMEI)并等待服务器的回答(消息'01'),在接收到(消息'01')后,客户端将数据发送到服务器,服务器将它们存储在数据库。在这里它只适用于一个客户端,甚至连接了多个客户端,但是如果服务器等待数据 GPS 客户端 (A) 时,另一个客户端 (B) 连接,那么服务器在接收到数据 GPS 时会出现问题客户端 (A) ,他将使用客户端 (B) 的名称存储它们,因为在我的代码中,服务器使用最后连接的客户端的名称存储 GPS 数据。

 <?php
error_reporting(E_ALL);
set_time_limit(0);

$ip='192.168.1.1';
$port=135;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) ;
socket_bind($sock, $ip , $port) ;

socket_listen ($sock);

echo "Waiting for incoming connections... \n";
$tab=array();
$client = array($sock);
while (true)
{
              $read = $client;
              $write=null ;
              $except=null ;
 if(socket_select($read , $write , $except , 0)<1)
              continue;

 if (in_array($sock, $read))
  {
              $client[] = $newsock = socket_accept($sock);
              socket_getpeername($newsock, $address, $port) ;
              echo "Client $address : $port is now connected to us. \n";
              echo "Waiting for incoming data... \n";
              $key = array_search($sock, $read);
              unset($read[$key]);
     }

  foreach ($read as $read_sock)
   {
    $input = socket_read($read_sock, 102401,PHP_BINARY_READ) ;

     if ($input == false)
       {
           $key = array_search($read_sock, $client);
           unset($client[$key]);
           echo "client disconnected.\n";
           continue;
        }
        if(!empty($input))
        {
            if(strlen($input)==17){
            //$input=bin2hex($input) ;
            $input = substr($input,2,strlen($input));

            $imei_verif=$input ;
            echo "Le 1er socket reçu : \n";
            echo  "IMEI =$input"."\n"."length=".strlen($input);


            //$response=hex2bin('01');
            $response='01';
            echo "\n La réponse est 01 \n";
           $len = strlen($response);

           $res_write=socket_send($read_sock ,$response,$len,0);
           echo "GPS informations ....... \n";
           $conn = new mysqli("localhost", "root", "", "fma120");
           $result = $conn->query("SELECT * FROM vehicule WHERE imei='$imei_verif'");
           $outp = $result->fetch_assoc();
           $identifiant= $outp['identifiant'] ;
           $matricule=$outp['matricule'];
           echo $identifiant."\n";
           echo $matricule;
            }



            else {
                echo "Client $address : $port is now connected to us. \n";
                $input=bin2hex($input) ;
                $payload=$input ;
                $crc = substr($payload, strlen($payload) - 8, 8);

                echo "crc = ".$crc ."\n" ;

                $avlDataWithChecks = substr($payload, 16, -8);
                if (substr($avlDataWithChecks, 2, 2) !== substr($avlDataWithChecks, strlen($avlDataWithChecks) - 2, 2))
                {
                    echo "First element count check is different than last element count check \n ";
                    echo " informations = " .$input ."\n" ;

                }

                if(substr($avlDataWithChecks, 2, 2) == substr($avlDataWithChecks, strlen($avlDataWithChecks) - 2, 2) )
                {

                    echo"voila ....................... GPS informations réçu : \n" ;

                    echo"input = ".$input ."\n" ;
                    echo "length=".strlen($input);
                    echo "avlDataWithChecks = ".$avlDataWithChecks ."\n" ;

                    $numberOfElements = hexdec(substr($avlDataWithChecks, 2, 2));
                    echo "numberOfElements = " .$numberOfElements ."\n" ;
                    $avlData = substr($avlDataWithChecks, 4, -2);


                    $position = 0;
                    $resultData = [];


                    $dateTime = new DateTime();
                    $timestamp = hexdec(substr($avlData, $position, 16)) / 1000;
                    $timestamp+= 7200;
                    echo "timestamp =".$timestamp . "\n";
                    $dateTime -> setTimestamp(intval($timestamp));
                    echo "dateteTime = " .$dateTime->format('U = Y-m-d H:i:s') . "\n";
                    $position += 16;



                    $priority = (int)hexdec(substr($avlData, $position, 2));
                    echo" priority = " .$priority ."\n" ;
                    $position += 2;

                    $longitude = substr($avlData, $position, 8);
                    $longitude = (float)(hexdec($longitude) / 10000000);
                    echo" longitude = " .$longitude ."\n" ;
                    $position += 8;

                    $latitude = substr($avlData, $position, 8);
                    $latitude = (float)(hexdec($latitude) / 10000000);
                    echo" latitude = " .$latitude ."\n" ;
                    $position += 8;

                    $altitude = (int)hexdec(substr($avlData, $position, 4));
                    echo" altitude = " .$altitude ."\n" ;
                    $position += 4;

                    $angle = (int)hexdec(substr($avlData, $position, 4));
                    echo" angle = " .$angle ."\n" ;
                    $position += 4;

                    $satellites = (int)hexdec(substr($avlData, $position, 2));
                    echo" satellites = " .$satellites ."\n" ;
                    $position += 2;

                    $speed = (int)hexdec(substr($avlData, $position, 4));
                    echo" speed = " .$speed ."\n" ;

                    echo "\n La réponse est $numberOfElements \n";


                    $output = sprintf("%08X", $numberOfElements) ;
                    $output2=$output ;
                    echo "avant l'envoie = " .$output2 ."\n" ;

                    $output2 =hex2bin($output2);


                    $len = strlen($output2);

                    $res_write=socket_send($read_sock,$output2,$len,0);

                    echo "apres l'envoie = " .$output2 ."\n" ;
                    //echo $identifiant."\n";
                    //$output2 =hex2bin($output2);


                    $len = strlen($output2);

                    $res_write=socket_send($read_sock ,$output2,$len,0);

                    echo "apres l'envoie = " .$output2 ."\n" ;
                    try
                    {
                        $bdd = new PDO('mysql:host=localhost;dbname=fma120;charset=utf8', 'root', '');
                    }
                    catch(Exception $e)
                    {
                        die('Erreur : '.$e->getMessage());
                    }
                    $bdd->exec("INSERT INTO gps_data(client,matricule,imei, timestamp, longitude, latitude, altitude,angle,satellites,speed,etat,n_of_elem) VALUES('$identifiant','$matricule','$imei_verif', '$timestamp', '$longitude', '$latitude', '$altitude','$angle','$satellites','$speed','0','$numberOfElements')");


                    echo 'Strored in the data base';
                }
                }
        }
    }
}

?>

【问题讨论】:

    标签: php sockets


    【解决方案1】:

    您需要存储数据之间的关系,例如,您可以创建一个以唯一客户端 ID 作为键的多数组。您还需要存储客户端 ID 客户端套接字关系。

    实现起来并不难,但要保持所有相关的东西井井有条,所以尽可能简单(我建议创建一个包含每个连接相关信息的类,所以你不要迭代抽象套接字资源,而是迭代在逻辑连接对象上,这使得它更容易处理)

    例如

    class ConnectionInfo
    {
        public $Socket;
        public $ID;
    }
    

    $Connections = [];
    $Connections[$ID]['Socket'] = $Socket;
    

    【讨论】:

      【解决方案2】:

      问题是 gps 数据不包含有关客户端的任何信息,因此我无法知道此数据与哪个客户端有关,也许如果我知道消息源的 ip 地址并在将其与 ip 地址进行比较之后第一次连接的时间(当客户端发送imei时)。
      我可以存储第一个连接的 ID,但我如何知道同一客户端的第二个连接的信息以将它们与存储的 id 进行比较?

      【讨论】:

        猜你喜欢
        • 2014-03-10
        • 2021-10-26
        • 2014-11-05
        • 2018-06-10
        • 1970-01-01
        • 2013-11-11
        • 1970-01-01
        • 2023-04-05
        • 2011-11-15
        相关资源
        最近更新 更多