Saluti a tutti e grazie per l'aiuto
Ho creato un socket_ server in loop, inizialmente senza un termine di chiusura,
Questo si collega ad una porta e si mette in ascolto per ricevere dei dati da alcuni dispositivi.
Il problema nasce dal fatto che vorrei poter sospendere il socket attraverso il cambiamento di un flag su un database.
Questo purtroppo non avviene il socket continua il suo loop, non fa lo shutdown e la chiusura, tutti i comandi, non vengono eseguiti, cosa sbaglio?
include iniziale (fa una semplice select e presenta la variabile nell'array:
Ho creato un socket_ server in loop, inizialmente senza un termine di chiusura,
Questo si collega ad una porta e si mette in ascolto per ricevere dei dati da alcuni dispositivi.
Il problema nasce dal fatto che vorrei poter sospendere il socket attraverso il cambiamento di un flag su un database.
Questo purtroppo non avviene il socket continua il suo loop, non fa lo shutdown e la chiusura, tutti i comandi, non vengono eseguiti, cosa sbaglio?
PHP:
<?php
$sql ="SELECT socketalive from semaphores";
include 'database-select.php';
var_dump($v);
if ($v==0) {
/********Socket Server*********************/
set_time_limit (0);
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;
// Create a TCP Stream socket
//$sock = socket_create(AF_INET, SOCK_STREAM, 0); // 0 for SQL_TCP oppure IPPROTO_IP questo è il protocollo IP
if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Creazione del socket non riuscita: [$errorcode] $errormsg \n");
}
echo "{$v}";
// Bind the socket to an address/port
socket_bind($sock, 0, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while ($v==0):
var_dump($v); // QUESTO COMANDO NON PRODUCE ALCUN RISULTATO
//Si connette al database per controllare il valore di socketalive
$servername = "localhost";
$username = "root";
$password = "Gionewday63";
$dbname = "wandw";
$sql ="SELECT socketalive from semaphores";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new Rows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {}
if ($v==0) {
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
// Read the input from the client – 1024000 bytes
$input = socket_read($client, 1290);
$output = trim($input);
$myfile = fopen("testfile.txt", "a+");
fwrite($myfile, $v);
fclose($myfile);
/*
Strip all white spaces from input
$output .= \n\r;
$output = ereg_replace("[ \t\n\r]","",$input)."\0";
$message=explode(',',$output);
$file = 'gps.txt';
Open the file to get existing content
$current = file_get_contents($file);
//Append new data to the file
$current .= $output;
// Write the contents back to the file
file_put_contents($file, $current);
//socket_shutdown($sock, 2);
//($sock);
*/
}
else {
echo "sono passato da qui";
socket_shutdown($sock, 2);
socket_close($sock);
//break;
}
$i++;
endwhile;
}
else {
echo ("Questo script finisce qui");
}
?>
include iniziale (fa una semplice select e presenta la variabile nell'array:
PHP:
<?php
class Rows extends RecursiveIteratorIterator {
}
//Si connette al database per controllare il valore di socketalive
$servername = "localhost";
$username = "root";
$password = "Gionewday63";
$dbname = "wandw";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($sql);
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new Rows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
Ultima modifica di un moderatore: