I'm busy with a tracking script to handle connections and read the information from a tracking unit. But the way the manufacturer explained it to me, is that the unit sends the information like a URL and it connects to a TCP port, for example: 7203.
Now my question is: how do I get the headers with a unit that connects to my server, when working with a port? And I also run the code from a Linux Command line.
Below is the code I use to open a port:
#!/usr/bin/php -q
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_time_limit(0);
ob_implicit_flush();
$ip = 'IP of my server';
$port = '7203';
$__server_listening = true;
declare(ticks = 1);
become_daemon();
/* nobody/nogroup, change to your host's uid/gid of the non-priv user
** Comment by Andrew - I could not get this to work, i commented it out
the code still works fine but mine does not run as a priv user anyway....
uncommented for completeness
*/
change_identity(99, 99);
/* handle signals */
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGINT, 'sig_handler');
pcntl_signal(SIGCHLD, 'sig_handler');
/* change this to your own host / port */
server_loop($ip, $port);
/**
* Change the identity to a non-priv user
*/
function change_identity( $uid, $gid )
{
if( !posix_setgid( $gid ) )
{
print "Unable to setgid to " . $gid . "!\n";
exit;
}
if( !posix_setuid( $uid ) )
{
print "Unable to setuid to " . $uid . "!\n";
exit;
}
}
/**
* 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 $__server_listening;
if(($sock = socket_create(AF_INET, SOCK_STREAM, 0)) < 0)
{
echo "failed to create socket: ".socket_strerror($sock)."\n";
exit();
}
if(($ret = socket_bind($sock, $address, $port)) < 0)
{
echo "failed to bind socket: ".socket_strerror($ret)."\n";
exit();
}
if( ( $ret = socket_listen( $sock, 0 ) ) < 0 )
{
echo "failed to listen to socket: ".socket_strerror($ret)."\n";
exit();
}
socket_set_nonblock($sock);
echo "waiting for clients to connect on $address:$port\n";
while ($__server_listening)
{
$connection = @socket_accept($sock);
if ($connection === false)
{
usleep(100);
}
elseif ($connection > 0)
{
handle_client($sock, $connection);
}
else
{
echo "error: ".socket_strerror($connection);
die;
}
}
}
/**
* Signal handler
*/
function sig_handler($sig)
{
switch($sig)
{
case SIGTERM:
case SIGINT:
//exit();
break;
case SIGCHLD:
pcntl_waitpid(-1, $status);
break;
}
}
/**
* Handle a new client connection
*/
function handle_client($ssock, $csock)
{
GLOBAL $__server_listening;
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
die;
}
elseif ($pid == 0)
{
/* child process */
$__server_listening = false;
socket_getpeername($csock, $remip, $remport);
print date("d-m-y H:i:s") . " Connection from $remip:$remport\r\n\r\n";
socket_close($ssock);
interact($csock);
socket_close($csock);
print date("d-m-y H:i:s") . " Connection to $remip:$remport closed\r\n\r\n";
print "------------------------------------------------------------------------------------------------------------------------------------------\r\n\r\n";
}
else
{
socket_close($csock);
}
}
function interact($socket)
{
$gets = $_REQUEST;
print $gets;
}
/**
* Become a daemon by forking and closing the parent
*/
function become_daemon()
{
$pid = pcntl_fork();
if ($pid == -1)
{
/* fork failed */
echo "fork failure!\n";
exit();
}
elseif ($pid)
{
/* close the parent */
exit();
}
else
{
/* child becomes our daemon */
posix_setsid();
//chdir('/');
//umask(0);
return posix_getpid();
}
}
?>
Where the function interact is, is where the connection is already made and I should be able to read the headers. But no joy. The error I receive is: Notice: Array to string conversion in /home/armand/bin/7203/7203.php on line 198
And the information that the unit sends looks like this:
357671030507047#V500#0000#AUTOLOW#1#00ab46cb,0,0,0,2,09,14,$GPRMC,073106.000,A,2647.0278,S,02750.8628,E,0.00,324.27,021213,,,A*7C##`
You can use the built-in http server in PHP with this command
or if you use an apache http server, in the configuration file use