I need to handle continuous data through TCP and write it to database. I use the following code (suggested by ZZ Coder):
<?php
$fp = fsockopen ("192.168.0.179", 2323);
if (!$fp) {
die ("$errstr ($errno)");
}
if ($fp) {
$user="root";
$password="12345";
$database="db_name";
mysql_connect('localhost',$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$point=false;
$status="";
$fix=0;
while (!$point) {
$string=@fgets($fp, 4096);
switch (substr($string,0,6)) {
case "\$GPRMC" :
list($sentence, $time, $status, $latitude, $NS, $longitude, $EW, $speed, $course, $date, $magvar, $magvarEW)= explode(",", trim($string));
mysql_query("INSERT INTO gps_data (time, course) VALUES ('$time', '$course')");
break;
case "\$GPGGA" :
list($sentence, $time, $latitude, $NS, $longitude, $EW, $fix, $nbsat, $HDOP, $altitude,,,,,)= explode(",", trim($string));
break;
default :
break;
}
if ($status=="A" and $fix == 1){
$point=true;
}
}
fclose ($fp);
mysql_close();
}
else {
print "Fatal error\n";
}
?>
Questions
How do I start and stop this server?
How do I make it work nonstop (hours/days/weeks) because right now it stops executing at some point.
What happens if
10/20/100/1000
trackers connect and start sending data to192.168.0.179:2323
(server)?