0

I am new to socket programming. I have a GPS Tracker that connects and send data to a defined public server:port through GPRS connection. I am getting the result on the terminal.

I want to know how can I get that response in some variable so I can save those entries in database real-time.

Here is the code.

require_once("SocketServer.class.php"); // Include the File
$server = new SocketServer("xxx.xx.x.xxx",8153); // 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
$server->infinite_loop(); // Run Server Code Until Process is terminated.


function handle_connect(&$server,&$client,$input)
{
    SocketServer::socket_write_smart($client->socket,"String? ","");
}
function handle_input(&$server,&$client,$input)
{
    // You probably want to sanitize your inputs here
    $trim = trim($input); // Trim the input, Remove Line Endings and Extra Whitespace.

    if(strtolower($trim) == "quit") // User Wants to quit the server
    {
        SocketServer::socket_write_smart($client->socket,"Oh... Goodbye..."); // Give the user a sad goodbye message, meany!
        $server->disconnect($client->server_clients_index); // Disconnect this client.
        return; // Ends the function
    }

    $output = strrev($trim); // Reverse the String
echo "output is".$trim;
    SocketServer::socket_write_smart($client->socket,$output); // Send the Client back the String
    SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
}

SocketServer.class.php

http://www.phpclasses.org/browse/file/31975.html

This line doesn't print the result on terminal

echo "output is".$trim;

I have tried this as well

$myfile = fopen("file.txt", "w") or die("Unable to open file!");
   fwrite($myfile, $trim);
fclose($myfile);

and the file remain empty

user1hjgjhgjhggjhg
  • 1,237
  • 4
  • 15
  • 34
  • you mean like it already is `$trim` is a variable and there for the content is in a variable. so after `$trim` save it in your database. Open your Database connection at the top of the file before the functions and use it inside the functions using `global $dbConnection;` then `$dbConnection->prepair()` / `$dbConnection->query()` – Barkermn01 May 05 '18 at 11:35
  • I know how to do with database. but when I do this echo "output is".$trim; it doesn't show "output is" and then the result – user1hjgjhgjhggjhg May 05 '18 at 11:42
  • You do know that package is far old to be usable now don't you?, `7 years ago` your far better off using a library that supports Async as PHP 7 does, `https://github.com/dazzle-php/socket` – Barkermn01 May 05 '18 at 12:49

1 Answers1

1

Go to SocketServer.class.php and look for this code

enter image description here

the result is in this variable $input. So if you want to save data into database then add your code in else part.

        if($input == null){
            $this->disconnect($i);
        }else{
            SocketServer::debug("{$i}@{$this->clients[$i]->ip} --> {$input}");
            $data= $input;           
            //add your code here

Note: The author has updated the code of its library. Here is the new code https://github.com/navarr/Sockets