How to release socket on the MAC OS X terminal after cancelling php code execution?

Asked

Viewed 182 times

0

I have a PHP executable with the code below:

<?php
require_once("SocketServer.class.php"); // Include the File
$server = new SocketServer("172.17.0.243",20490); // Create a Server binding to the given ip address and listen to port 31337 for connections
$server->max_clients = 10; // Allow no more than 10 people to connect at a time
$server->hook("CONNECT","handle_connect"); // Run handle_connect every time someone connects
$server->hook("INPUT","handle_input"); // Run handle_input whenever text is sent to the server
$GLOBALS['conexao'] = mysqli_connect('127.0.0.1','root','','bd');
//$conexao = mysqli_connect('127.0.0.1','root','','bd') or die("Some error occurred during connection " . mysqli_error($conexao));  
echo "Conectado ao BD.";
$server->infinite_loop(); // Run Server Code Until Process is terminated.
//


function handle_connect($server,$client,$input)
{
    SocketServer::socket_write_smart($client->socket,"OK!");
}
function handle_input($server,$client,$input)
{
    date_default_timezone_set('Brazil/East');
    $dataagora = date("d-m-Y H:i:s");
    echo "Data: ".$dataagora;
    echo "\nInput = ".$input."!";
    $tamanho = strlen($input);
    $str = $input;
    $parts = explode(',',$str);
    $ano_4digitos = (int)date("Y");
    $ano_2digitos = (int)date("y");
    $diferenca = $ano_4digitos-$ano_2digitos;
    $diferenca = $diferenca/100;
    $data = (string)$diferenca.$parts[11]{4}.$parts[11]{5}."-".$parts[11]{2}.$parts[11]{3}."-".$parts[11]{0}.$parts[11]{1}." ".$parts[3]{0}.$parts[3]{1}.":".$parts[3]{2}.$parts[3]{3}.":".$parts[3]{4}.$parts[3]{5};
    echo "\nData de hoje segundo o GPS: ".$data."\n";
    $hora_menos_tres = "";
    $hora_menos_tres += $parts[3]{0}.$parts[3]{1};
    $hora_menos_tres = intval($hora_menos_tres);
    $hora_menos_tres -= 3;
    echo "\nHora correta de hoje: ".$hora_menos_tres."\n";
    $data = (string)$diferenca.$parts[11]{4}.$parts[11]{5}."-".$parts[11]{2}.$parts[11]{3}."-".$parts[11]{0}.$parts[11]{1}." ".$hora_menos_tres.":".$parts[3]{2}.$parts[3]{3}.":".$parts[3]{4}.$parts[3]{5};
    echo "\nData correta de hoje: ".$data."\n";
    $query_sql_insert = "INSERT INTO coordenada (`imei`, `data`, `latitude`, `longitude`, `estado`) VALUES (\"".$parts[1]."\", \"".$data."\", ".$parts[5].", ".$parts[7].", \""."P"."\")";
    $query = mysqli_query($GLOBALS['conexao'], $query_sql_insert);
    echo "\n".$query_sql_insert."\n";
}

This executable keeps running and taking data from a gps via GPRS and inserting it into SQL. It works perfectly, but it turns out that, as I am testing, I need to cancel the execution of the code from time to time and give a control+c to it (there is a close to the socket).

When I cancel, fix something and try to run quickly after, this error appears:

Warning: socket_bind(): unable to bind address [48]: Address already in use in /Applications/XAMPP/xamppfiles/htdocs/SocketServer.class.php on line 53
Issue Binding

Is there some kind of command that releases ip and port quickly? I saw something about lsof -i :20490 but shows no process occupying the door.

1 answer

1

You can try before calling socket_bind(), calling socket_close so that the process tries to close any previous connection. On line 53 of Socketserver.class.php is the following::

socket_bind($this->master_socket,$this->config["ip"],$this->config["port"]) or die("Issue Binding");

You can then put up:

socket_close($this->master_socket);

Or else:

socket_shutdown($this->master_socket);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.