How to free socket on MAC OS X terminal after canceling php code execution?

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 is running and picking data from a gps via GPRS and inserting into SQL. It works perfectly, but it turns out that as I'm testing, I need to cancel the execution of the code from time to time and I give a + c control for this (there is no close for the socket).

When I cancel, I correct 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 any kind of command that frees the ip and port quickly? I have seen something about lsof -i :20490 but it does not show any process occupying the port.

    
asked by anonymous 21.03.2014 / 20:54

1 answer

1

You can try before calling socket_bind (), call socket_close so that the process tries to close any previous connection. At 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:

socket_shutdown($this->master_socket);
    
23.03.2014 / 00:18