Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

bruno nozé

socket converter ascii em hex

Recommended Posts

boa tarde, Preciso de Ajuda com este soquete Que Estou modificando para Receber Dados em ascii e conversor parágrafo hex, o código ESTA ASSIM:

 

#!/usr/bin/php -q
<?php
//waiting for system startup
//crontab: @reboot php -q /var/www/server/tracker.php
//sleep (180);
/**
* Listens for requests and forks on each connection
*/
$tipoLog = "arquivo"; // tela //debug log, escreve na tela ou no arquivo de log.
$fh = null;
$remip = null;
$remport = null;
$imei = '';
function abrirArquivoLog($imeiLog) {
GLOBAL $fh;
//$fn = ".".dirname(__FILE__)."/sites/1/logs/Log_". trim($imeiLog) .".log";
$fn = "./var/www/sites/1/logs/Log_". trim($imeiLog) .".log";
$fn = trim($fn);
$fh = fopen($fn, 'a') or die ("Can not create file");
$tempstr = "Log Inicio".chr(13).chr(10);
fwrite($fh, $tempstr);
}
function fecharArquivoLog() {
GLOBAL $fh;
if ($fh != null)
fclose($fh);
}
function printLog( $fh, $mensagem ) {
GLOBAL $tipoLog;
GLOBAL $fh;
if ($tipoLog == "arquivo") {
//escreve no arquivo
if ($fh != null)
fwrite($fh, $mensagem.chr(13).chr(10));
} else {
//escreve na tela
echo $mensagem."<br />";
}
}
// IP Local
//$ip = $dataIp['valor'];
$ip = '23.89.198.254';
// Port
//$port = $dataPorta['valor'];
$port = '10011';
// Path to look for files with commands to send
$command_path = "./var/www/sites/1/";
//mysql_close($cnx);
$__server_listening = true;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
declare(ticks = 1);
ini_set('sendmail_from', $from_email);
server_loop($ip, $port);
/**
* Creates a server socket and listens for incoming client connections
* @param string $address The address to listen on
* @param int $port The port to listen on
*/
function server_loop($address, $port) {
GLOBAL $fh;
GLOBAL $__server_listening;
printLog($fh, "server_looping...");
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0) {
printLog($fh, "failed to create socket: ".socket_strerror($sock));
exit();
}
if(($ret = socket_bind($sock, $address, $port)) < 0) {
printLog($fh, "failed to bind socket: ".socket_strerror($ret));
exit();
}
if( ( $ret = socket_listen( $sock, 0 ) ) < 0 ) {
printLog($fh, "failed to listen to socket: ".socket_strerror($ret));
exit();
}
socket_set_nonblock($sock);
printLog($fh, "waiting for clients to connect...");
while ($__server_listening) {
$connection = @socket_accept($sock);
if ($connection === false) {
usleep(100);
} elseif ($connection > 0) {
handle_client($sock, $connection);
} else {
printLog($fh, "error: ".socket_strerror($connection));
die;
}
}
}
function interact($socket) {
GLOBAL $fh;
GLOBAL $command_path;
GLOBAL $firstInteraction;
GLOBAL $remip;
GLOBAL $remport;
$loopcount = 0;
$conn_imei = "";
/* TALK TO YOUR CLIENT */
$rec = "";
// Variavel que indica se comando est? em banco ou arquivo.
$tipoComando = "arquivo"; //"arquivo";
# Some pacing to ensure we don't split any incoming data.
sleep (1);
# Timeout the socket if it's not talking...
# Prevents duplicate connections, confusing the send commands
$loopcount++;
if ($loopcount > 120) return;
#remove any whitespace from ends of string.
$rec = trim($rec);
/* M?DULO IMEI GEN?RICO */
$isGIMEI = true;
$loopcount = 0;
$hex = ascii2hex($rec);
printLog($fh, date("d-m-y h:i:sa") . " Got : $hex");
} //fim interact
function ascii2hex($rec) {
$hex = '';
for ($i = 0; $i < strlen($rec); $i++) {
$byte = strtoupper(dechex(ord($rec{$i})));
$byte = str_repeat('0', 2 - strlen($byte)).$byte;
$hex.=$byte." ";
}
return $hex;
}
?>
Preciso Que salve em hum Arquivo de log, mas nao deu Certo alguem PODE me Ajudar !!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.