1

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, thx.

<?php

error_reporting(E_ALL);

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

// Server IP address
$address = "xxx.xxx.xxx.xx";
// 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; 
    } 
} 
?> 
2
Contributors
5
Replies
21
Views
3 Years
Discussion Span
Last Post by accra
0

I tested it and every thing seems fine. Pls confirm this: do i have to point to the main ip eg. www.anywhere.com or i will have to point to www.anywhere.com/gps_listener.php?

1

Sorry for mix-up.
Now am getting a real error to work with.

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

from what i gathered online, i have to enble

;extension=php_sockets.dll

in php.ini by removing this ; like this

extension=php_sockets.dll

. Others are also saying if its not in php.ini, by coping and pasting it in php.ini will solve the problem and i get all that to this point.

my porblem now is this, my script is online, how do i enble

extension=php_sockets.dll

on my online server, or i have to upload php.ini with extension enble to my server online?

0

this is the revised script

<?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.log', $buf);
            //echo "$buf\n";
        }

    }        
} while (true);

socket_close($sock);

?> 
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.