6 Replies - 264 Views - Last Post: 03 August 2014 - 04:22 PM

#1 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 19-February 11

GPS script on server not receiving gps data

Posted 02 August 2014 - 02:28 AM

Hey guys, i found this piece of code that is suppose to read a port on my server and write to a .txt file the gps data it get gets.

am receiving nothing, am not not sure what the problem is, also am new to gps and php socket programming... pls help out, thanks.

<?php
error_reporting(E_ALL);

/* Permitir al script esperar para conexiones. */
set_time_limit(0);

/* Activar el volcado de salida implícito, así veremos lo que estamo obteniendo
* mientras llega. */
ob_implicit_flush();

$address = 'xxx.xxx.xxx.xx';
$port = 80;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;
    
    $read = array_merge($read,$clients);
    
    // Set up a blocking call to socket_select
    if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1)
    {
        //    SocketServer::debug("Problem blocking socket_select?");
        continue;
    }
    
    // Handle new Connections
    if (in_array($sock, $read)) {        
        
        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);
        /* Enviar instrucciones. */
        $msg = "\nBienvenido al Servidor De Prueba de PHP. \n" .
        "Usted es el cliente numero: {$key[0]}\n" .
        "Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));
        
    }
    
    // Handle Input
    foreach ($clients as $key => $client) { // for each client        
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                unset($clients[$key]);
                socket_close($client);
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($client);
                break 2;
            }
            $talkback = "Cliente {$key}: Usted dijo '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));

            // write received gprs data to the file
			writeToFile('gprs.txt', $buf);
            //echo "$buf\n";
        }
        
    }        
} while (true);

socket_close($sock);


// error_reporting(E_ALL);

// /* Allow the script to hang around waiting for connections. */ 
// set_time_limit(0);

// // Server IP address
// $address = "162.210.101.52";
// // Port to listen
// $port = 443;

// $mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// socket_bind($mysock,$address, $port) or die('Could not bind to address'); 
// //socket_listen($mysock, 5);
// socket_listen($mysock);
// $client = socket_accept($mysock);

// // read 1024 bytes from client
// $input = socket_read($client, 1024);

// // write received gprs data to the file
// writeToFile('gprs.txt', $input);

// socket_close($client);
// socket_close($mysock);
?> 

<?php
// function writeToFile($strFilename, $strText) { 
//     if($fp = @fopen($strFilename,"w"))  { 
//           $contents = fwrite($fp, $strText); 
//           fclose($fp); 
//           return true; 
//     } else { 
//       return false; 
//     } 
// } 
?> 

This post has been edited by dannybarh: 02 August 2014 - 07:10 AM


Is This A Good Question/Topic? 0
  • +

Replies To: GPS script on server not receiving gps data

#2 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 19-February 11

Re: GPS script on server not receiving gps data

Posted 02 August 2014 - 09:31 AM

lol, 33 views is enough for some one to say hi at lest.

am getting this error
Fatal error: Call to undefined function socket_create() in /home/www/domain.com/gps_listener.php on line 14

I need to enable in php.ini
extension=php_sockets.dll
to solve the issue. My script is online, so how do i enable it online?

Here is my revised code

<?php
error_reporting(E_ALL);

/* Permitir al script esperar para conexiones. */
set_time_limit(0);

/* Activar el volcado de salida implícito, así veremos lo que estamo obteniendo
* mientras llega. */
ob_implicit_flush();

$address = 'xxx.xxx.xxx.xx';
$port = 80;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;

    $read = array_merge($read,$clients);

    // Set up a blocking call to socket_select
    if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1)
    {
        //    SocketServer::debug("Problem blocking socket_select?");
        continue;
    }

    // Handle new Connections
    if (in_array($sock, $read)) {        

        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);
        /* Enviar instrucciones. */
        $msg = "\nBienvenido al Servidor De Prueba de PHP. \n" .
        "Usted es el cliente numero: {$key[0]}\n" .
        "Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));

    }

    // Handle Input
    foreach ($clients as $key => $client) { // for each client        
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                unset($clients[$key]);
                socket_close($client);
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($client);
                break 2;
            }
            $talkback = "Cliente {$key}: Usted dijo '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));

            // write received gprs data to the file
            writeToFile('gprs.txt', $buf);
            //echo "$buf\n";
        }

    }        
} while (true);

socket_close($sock);

?> 

This post has been edited by dannybarh: 02 August 2014 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 766
  • View blog
  • Posts: 3,349
  • Joined: 03-December 12

Re: GPS script on server not receiving gps data

Posted 02 August 2014 - 04:55 PM

Quote

I need to enable in php.ini
1	extension=php_sockets.dll

to solve the issue. My script is online, so how do i enable it online?


That depends, is this on "your server" or a hosted shared account? If you edit the ini file, you will need to restart the server. So, if it is a shared hosting, your out of luck unless something major happens and they need to (which isn't likely) or it is on your server, vps, dedicated, localhost... Then, just restart Apache if that is what you are running.
Was This Post Helpful? 1
  • +
  • -

#4 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 19-February 11

Re: GPS script on server not receiving gps data

Posted 03 August 2014 - 07:48 AM

Is there another function for creating php sockets apart from this
socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
?

Really need to know, thanks.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: GPS script on server not receiving gps data

Posted 03 August 2014 - 08:34 AM

Why do you ask? Why is socket_create not working for you?

Of the top of my head, stream_socket_server can be used to create a socket server/listener.
Was This Post Helpful? 0
  • +
  • -

#6 dannybarh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 65
  • Joined: 19-February 11

Re: GPS script on server not receiving gps data

Posted 03 August 2014 - 02:33 PM

it is not working, it has disable on my my share host server. I will give this try and get back. Am using this script to receive data from a gps tracker. I you know any script that can work , i will be grateful, thanks.

<?php
error_reporting(E_ALL);

/* Permitir al script esperar para conexiones. */
set_time_limit(0);

/* Activar el volcado de salida implícito, así veremos lo que estamo obteniendo
* mientras llega. */
ob_implicit_flush();

$address = 'xxx.xxx.xxx.xx';
$port = 443;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() falló: razón: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;
    
    $read = array_merge($read,$clients);
    
    // Set up a blocking call to socket_select
    if(socket_select($read,$write = NULL, $except = NULL, $tv_sec = 5) < 1)
    {
        //    SocketServer::debug("Problem blocking socket_select?");
        continue;
    }
    
    // Handle new Connections
    if (in_array($sock, $read)) {        
        
        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);
        /* Enviar instrucciones. */
        $msg = "\nBienvenido al Servidor De Prueba de PHP. \n" .
        "Usted es el cliente numero: {$key[0]}\n" .
        "Para salir, escriba 'quit'. Para cerrar el servidor escriba 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));
        
    }
    
    // Handle Input
    foreach ($clients as $key => $client) { // for each client        
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() falló: razón: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                unset($clients[$key]);
                socket_close($client);
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($client);
                break 2;
            }
            $talkback = "Cliente {$key}: Usted dijo '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));

            // write received gprs data to the file
			writeToFile('gprs.log', $buf);
            //echo "$buf\n";
        }
        
    }        
} while (true);

socket_close($sock);

?> 

This post has been edited by dannybarh: 03 August 2014 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: GPS script on server not receiving gps data

Posted 03 August 2014 - 04:22 PM

OK, so this question is basically a continuation of the thread you opened yesterday. I'm merging the two threads. Please try to keep the discussion in one thread, so people can keep up with it.


Bottom line here is that, if your host has chosen to disable socket creation, they won't leave some secondary method of doing that enabled. That would just be pure incompetence. You should resign yourself to the fact that your won't be able to open a socket on your host.

Your options would then be to either find a new host, or find another way to interact with this GPS device that doesn't require sockets.


P.S.
Also keep in mind that we don't just hand out scripts here. We help people learn what they need to in order to write scripts on their own.

This post has been edited by Atli: 03 August 2014 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1