简体   繁体   中英

client and server php socket

I work on a geolocation project with PHP, I use sockets I have a problem, I explain: the client (box GPS) sends its identifier (IMEI) and it waits for the answer of the server (message '01'), after receiving(message'01') the client sends the data to the server and the server stores them in the database. here it works well with only one client and even with several clients connected but the problem if at the time the server wait for the data GPS client (A), another client (B) connects, so the server when he receives the data GPS of client (A) , he will store them with the name of client (B) because in my code the server stores the GPS data with the name of the last connected client.

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

?>

well you need to store the relationship between the data eg you can create an multi array with the unique client ID as key. you also need to store the client ID <=> client socket relationship.

its not that hard to implement but to keep every related stuff organized, so make it as easy as possible ( i would recommend to create a class that holds every connection related information, so you dont iterate over abstract socket resources instead you iterate over logical connection object which makes it much easier to handle )

eg

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

or

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

the problem is the gps data does not contain any information about the client so i can not know this data is related to which client, maybe if i know the ip address of the message source and after I compare it with the ip address of the first connection (when the client sends the imei).
I can store the ID of the first connection but how can I know the information of the 2nd connection of the same client to compare them with the stored id ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM