BioMetric FingerPrint Sync Socket Error - PHP











up vote
1
down vote

favorite












I am using Matrix Cosec BioMetric devices for attendance management system. I have coded to sync biometric punches on realtime basis. But the listener services automatically get stopped.



The Error Log details are as follows:



-- Socket Err: 2018-11-08 10:56:30New Client Connected

-- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_connect' for 'CONNECT'

-- Socket Err: 2018-11-08 10:56:30<-- String?

-- Socket Err: 2018-11-08 10:56:300@103.92.200.253 --> [SND_EVT&91118&1&08112018&225421&101&490&0&40&0&&]

-- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:56:32<-- [ACK_EVT&91118&]

-- Socket Err: 2018-11-08 10:56:32<-- String?

-- Socket Err: 2018-11-08 10:56:32Client 0 from 103.92.200.253 Disconnecting

-- Socket Err: 2018-11-08 10:56:46New Client Connected

-- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_connect' for 'CONNECT'

-- Socket Err: 2018-11-08 10:56:46<-- String?

-- Socket Err: 2018-11-08 10:56:460@103.92.200.253 --> [SND_EVT&88248&1&08112018&225343&101&153&0&40&0&&]

-- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:56:48<-- [ACK_EVT&88248&]

-- Socket Err: 2018-11-08 10:56:48<-- String?

-- Socket Err: 2018-11-08 10:56:48Client 0 from 103.92.200.253 Disconnecting

-- Socket Err: 2018-11-08 10:56:53New Client Connected

-- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_connect' for 'CONNECT'

-- Socket Err: 2018-11-08 10:56:53<-- String?

-- Socket Err: 2018-11-08 10:56:530@103.92.200.253 --> [SND_EVT&88249&1&08112018&225350&101&148&0&40&0&&]

-- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:56:54<-- [ACK_EVT&88249&]

-- Socket Err: 2018-11-08 10:56:54<-- String?

-- Socket Err: 2018-11-08 10:56:54Client 0 from 103.92.200.253 Disconnecting

-- Socket Err: 2018-11-08 10:56:56New Client Connected

-- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_connect' for 'CONNECT'

-- Socket Err: 2018-11-08 10:56:56<-- String?

-- Socket Err: 2018-11-08 10:56:560@103.92.200.253 --> [SND_EVT&88250&1&08112018&225353&101&132&0&40&0&&]

-- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:56:58<-- [ACK_EVT&88250&]

-- Socket Err: 2018-11-08 10:56:58<-- String?

-- Socket Err: 2018-11-08 10:56:58New Client Connected

-- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_connect' for 'CONNECT'

-- Socket Err: 2018-11-08 10:56:58<-- String?

-- Socket Err: 2018-11-08 10:56:581@103.92.200.253 --> [SND_EVT&68845&1&08112018&225351&101&430&0&40&0&&]

-- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:57:00<-- [ACK_EVT&68845&]

-- Socket Err: 2018-11-08 10:57:00<-- String?

-- Socket Err: 2018-11-08 10:57:00Client 0 from 103.92.200.253 Disconnecting

-- Socket Err: 2018-11-08 10:57:00Client 1 from 103.92.200.253 Disconnecting

-- Socket Err: 2018-11-08 10:57:03New Client Connected

-- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_connect' for 'CONNECT' -

-- Socket Err: 2018-11-08 10:57:03<-- String?

-- Socket Err: 2018-11-08 10:57:030@103.92.200.253 --> [SND_EVT&88251&1&08112018&225400&101&478&0&40&0&&]

-- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_input' for 'INPUT'

-- Socket Err: 2018-11-08 10:57:05<-- [ACK_EVT&88251&]

-- Socket Err: 2018-11-08 10:57:05<-- String?

-- Socket Err: 2018-11-08 10:57:05Client 0 from 103.92.200.253 Disconnecting


Below is my code for tcpListenerService.php



<?php
require_once (dirname ( dirname ( __FILE__ ) ) . "/include/config.php");
require_once ( LIBRARY_PATH. "/SocketServer.class.php"); // Include the File
date_default_timezone_set('Asia/Kolkata');


if(!isset($_GET["ip"])||!isset($_GET['port']))
die("Enter Valid IP & POrt");

$ip = trim($_GET["ip"]);
$port=trim($_GET['port']);
$close = null;





if(isset($_GET["close"]))
$close=1;
$server = new SocketServer($ip,$port,$close); // Create a Server binding to the given ip address and listen to port 31337 for connections
//$server = new SocketServer("192.168.100.110",1500);
$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.

$result = explode("&", $trim);




//for date and time conversions
$dt = $result[3];
$year=substr($dt,4);
$month=substr($dt,2,2);
$date=substr($dt,0,2);
$ty=$result[4];
$hr=substr($ty,0,2);
$min=substr($ty,2,2);
$sec= substr($ty,4,5);


$date_time = date("Y-m-d", strtotime("$date-$month-$year")) . ' ' . date('H:i:s', strtotime("$hr:$min:$sec"));

//get the device IP

$device_ip=$client->{"ip"};


try {
// open the connection to the database - $host, $user, $password, $database should already be set
//connect to the master DB
$masterconn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD, MASTER_DB_NAME );


// did it work?
if (mysqli_connect_errno()) {
throw new Exception("Failed to connect to MySQL: " . mysqli_connect_error());
}


//get the appropriate company mapped for the device IP
$query=mysqli_query($masterconn,"SELECT c.company_id,d.company_db_name
FROM company_biometric_devices_ip c
INNER JOIN company_details d ON c.company_id = d.company_id
WHERE device_ip ='$device_ip'");

$devices=array();
//get the multiple companies for single device
while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
$devices = $row;

// did it has values?
if (!$devices) {
throw new Exception("No Company Found for the device [{$device_ip}]");
}

//loop and connect it to the company database
foreach($devices as $device){
$company_id=$device["company_id"];
$company_name=DB_PREFIX.$device['company_db_name'];


//connect with the company DB Insert the data into the biometrics table
$conn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD,$company_name);
if (mysqli_connect_errno()) {
throw new Exception("Failed to connect to Company Db [ $company_name]: " . mysqli_connect_error());
}


$stmt=mysqli_prepare($conn, "INSERT INTO employee_biometric (date_time,employee_id,ip,event_seq_no,event_id)
(SELECT '$date_time',$result[6],'$device_ip','$result[1]','$result[5]' FROM device_users WHERE ref_id='$result[6]');");

//$stmt = mysqli_prepare ($conn,"INSERT INTO employee_biometric (employee_id,date_time,ip,company_id) VALUES (?,?,?,?)" );
//mysqli_stmt_bind_param ($stmt, 'ssss',$result[6],$date_time,$client->{"ip"},$company_id);

if (!$res = mysqli_stmt_execute ( $stmt ))
throw new Exception("MySQL Error: " . mysqli_error ($conn));
if($res)
$summary_insert = "CALL ATTENDANCE_SUMMARY_INSERT($result[6],'{$date_time}')";
$query=mysqli_query($conn,$summary_insert);
SocketServer::socket_write_smart($client->socket,"[ACK_EVT&{$result[1]}&]");// Send the Client back the String
SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
if(mysqli_stmt_affected_rows($stmt)>0)
break;
}
}catch (Exception $e) {
SocketServer::log($e->getMessage()."String: $trim"." IP: $device_ip",6);
}


}


My server and device automatically get disconnected, how to rectify the same. I have checked in the particular port also in open condition.










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am using Matrix Cosec BioMetric devices for attendance management system. I have coded to sync biometric punches on realtime basis. But the listener services automatically get stopped.



    The Error Log details are as follows:



    -- Socket Err: 2018-11-08 10:56:30New Client Connected

    -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_connect' for 'CONNECT'

    -- Socket Err: 2018-11-08 10:56:30<-- String?

    -- Socket Err: 2018-11-08 10:56:300@103.92.200.253 --> [SND_EVT&91118&1&08112018&225421&101&490&0&40&0&&]

    -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:56:32<-- [ACK_EVT&91118&]

    -- Socket Err: 2018-11-08 10:56:32<-- String?

    -- Socket Err: 2018-11-08 10:56:32Client 0 from 103.92.200.253 Disconnecting

    -- Socket Err: 2018-11-08 10:56:46New Client Connected

    -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_connect' for 'CONNECT'

    -- Socket Err: 2018-11-08 10:56:46<-- String?

    -- Socket Err: 2018-11-08 10:56:460@103.92.200.253 --> [SND_EVT&88248&1&08112018&225343&101&153&0&40&0&&]

    -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:56:48<-- [ACK_EVT&88248&]

    -- Socket Err: 2018-11-08 10:56:48<-- String?

    -- Socket Err: 2018-11-08 10:56:48Client 0 from 103.92.200.253 Disconnecting

    -- Socket Err: 2018-11-08 10:56:53New Client Connected

    -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_connect' for 'CONNECT'

    -- Socket Err: 2018-11-08 10:56:53<-- String?

    -- Socket Err: 2018-11-08 10:56:530@103.92.200.253 --> [SND_EVT&88249&1&08112018&225350&101&148&0&40&0&&]

    -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:56:54<-- [ACK_EVT&88249&]

    -- Socket Err: 2018-11-08 10:56:54<-- String?

    -- Socket Err: 2018-11-08 10:56:54Client 0 from 103.92.200.253 Disconnecting

    -- Socket Err: 2018-11-08 10:56:56New Client Connected

    -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_connect' for 'CONNECT'

    -- Socket Err: 2018-11-08 10:56:56<-- String?

    -- Socket Err: 2018-11-08 10:56:560@103.92.200.253 --> [SND_EVT&88250&1&08112018&225353&101&132&0&40&0&&]

    -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:56:58<-- [ACK_EVT&88250&]

    -- Socket Err: 2018-11-08 10:56:58<-- String?

    -- Socket Err: 2018-11-08 10:56:58New Client Connected

    -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_connect' for 'CONNECT'

    -- Socket Err: 2018-11-08 10:56:58<-- String?

    -- Socket Err: 2018-11-08 10:56:581@103.92.200.253 --> [SND_EVT&68845&1&08112018&225351&101&430&0&40&0&&]

    -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:57:00<-- [ACK_EVT&68845&]

    -- Socket Err: 2018-11-08 10:57:00<-- String?

    -- Socket Err: 2018-11-08 10:57:00Client 0 from 103.92.200.253 Disconnecting

    -- Socket Err: 2018-11-08 10:57:00Client 1 from 103.92.200.253 Disconnecting

    -- Socket Err: 2018-11-08 10:57:03New Client Connected

    -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_connect' for 'CONNECT' -

    -- Socket Err: 2018-11-08 10:57:03<-- String?

    -- Socket Err: 2018-11-08 10:57:030@103.92.200.253 --> [SND_EVT&88251&1&08112018&225400&101&478&0&40&0&&]

    -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_input' for 'INPUT'

    -- Socket Err: 2018-11-08 10:57:05<-- [ACK_EVT&88251&]

    -- Socket Err: 2018-11-08 10:57:05<-- String?

    -- Socket Err: 2018-11-08 10:57:05Client 0 from 103.92.200.253 Disconnecting


    Below is my code for tcpListenerService.php



    <?php
    require_once (dirname ( dirname ( __FILE__ ) ) . "/include/config.php");
    require_once ( LIBRARY_PATH. "/SocketServer.class.php"); // Include the File
    date_default_timezone_set('Asia/Kolkata');


    if(!isset($_GET["ip"])||!isset($_GET['port']))
    die("Enter Valid IP & POrt");

    $ip = trim($_GET["ip"]);
    $port=trim($_GET['port']);
    $close = null;





    if(isset($_GET["close"]))
    $close=1;
    $server = new SocketServer($ip,$port,$close); // Create a Server binding to the given ip address and listen to port 31337 for connections
    //$server = new SocketServer("192.168.100.110",1500);
    $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.

    $result = explode("&", $trim);




    //for date and time conversions
    $dt = $result[3];
    $year=substr($dt,4);
    $month=substr($dt,2,2);
    $date=substr($dt,0,2);
    $ty=$result[4];
    $hr=substr($ty,0,2);
    $min=substr($ty,2,2);
    $sec= substr($ty,4,5);


    $date_time = date("Y-m-d", strtotime("$date-$month-$year")) . ' ' . date('H:i:s', strtotime("$hr:$min:$sec"));

    //get the device IP

    $device_ip=$client->{"ip"};


    try {
    // open the connection to the database - $host, $user, $password, $database should already be set
    //connect to the master DB
    $masterconn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD, MASTER_DB_NAME );


    // did it work?
    if (mysqli_connect_errno()) {
    throw new Exception("Failed to connect to MySQL: " . mysqli_connect_error());
    }


    //get the appropriate company mapped for the device IP
    $query=mysqli_query($masterconn,"SELECT c.company_id,d.company_db_name
    FROM company_biometric_devices_ip c
    INNER JOIN company_details d ON c.company_id = d.company_id
    WHERE device_ip ='$device_ip'");

    $devices=array();
    //get the multiple companies for single device
    while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
    $devices = $row;

    // did it has values?
    if (!$devices) {
    throw new Exception("No Company Found for the device [{$device_ip}]");
    }

    //loop and connect it to the company database
    foreach($devices as $device){
    $company_id=$device["company_id"];
    $company_name=DB_PREFIX.$device['company_db_name'];


    //connect with the company DB Insert the data into the biometrics table
    $conn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD,$company_name);
    if (mysqli_connect_errno()) {
    throw new Exception("Failed to connect to Company Db [ $company_name]: " . mysqli_connect_error());
    }


    $stmt=mysqli_prepare($conn, "INSERT INTO employee_biometric (date_time,employee_id,ip,event_seq_no,event_id)
    (SELECT '$date_time',$result[6],'$device_ip','$result[1]','$result[5]' FROM device_users WHERE ref_id='$result[6]');");

    //$stmt = mysqli_prepare ($conn,"INSERT INTO employee_biometric (employee_id,date_time,ip,company_id) VALUES (?,?,?,?)" );
    //mysqli_stmt_bind_param ($stmt, 'ssss',$result[6],$date_time,$client->{"ip"},$company_id);

    if (!$res = mysqli_stmt_execute ( $stmt ))
    throw new Exception("MySQL Error: " . mysqli_error ($conn));
    if($res)
    $summary_insert = "CALL ATTENDANCE_SUMMARY_INSERT($result[6],'{$date_time}')";
    $query=mysqli_query($conn,$summary_insert);
    SocketServer::socket_write_smart($client->socket,"[ACK_EVT&{$result[1]}&]");// Send the Client back the String
    SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
    if(mysqli_stmt_affected_rows($stmt)>0)
    break;
    }
    }catch (Exception $e) {
    SocketServer::log($e->getMessage()."String: $trim"." IP: $device_ip",6);
    }


    }


    My server and device automatically get disconnected, how to rectify the same. I have checked in the particular port also in open condition.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am using Matrix Cosec BioMetric devices for attendance management system. I have coded to sync biometric punches on realtime basis. But the listener services automatically get stopped.



      The Error Log details are as follows:



      -- Socket Err: 2018-11-08 10:56:30New Client Connected

      -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:30<-- String?

      -- Socket Err: 2018-11-08 10:56:300@103.92.200.253 --> [SND_EVT&91118&1&08112018&225421&101&490&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:32<-- [ACK_EVT&91118&]

      -- Socket Err: 2018-11-08 10:56:32<-- String?

      -- Socket Err: 2018-11-08 10:56:32Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:46New Client Connected

      -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:46<-- String?

      -- Socket Err: 2018-11-08 10:56:460@103.92.200.253 --> [SND_EVT&88248&1&08112018&225343&101&153&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:48<-- [ACK_EVT&88248&]

      -- Socket Err: 2018-11-08 10:56:48<-- String?

      -- Socket Err: 2018-11-08 10:56:48Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:53New Client Connected

      -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:53<-- String?

      -- Socket Err: 2018-11-08 10:56:530@103.92.200.253 --> [SND_EVT&88249&1&08112018&225350&101&148&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:54<-- [ACK_EVT&88249&]

      -- Socket Err: 2018-11-08 10:56:54<-- String?

      -- Socket Err: 2018-11-08 10:56:54Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:56New Client Connected

      -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:56<-- String?

      -- Socket Err: 2018-11-08 10:56:560@103.92.200.253 --> [SND_EVT&88250&1&08112018&225353&101&132&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:58<-- [ACK_EVT&88250&]

      -- Socket Err: 2018-11-08 10:56:58<-- String?

      -- Socket Err: 2018-11-08 10:56:58New Client Connected

      -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:58<-- String?

      -- Socket Err: 2018-11-08 10:56:581@103.92.200.253 --> [SND_EVT&68845&1&08112018&225351&101&430&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:57:00<-- [ACK_EVT&68845&]

      -- Socket Err: 2018-11-08 10:57:00<-- String?

      -- Socket Err: 2018-11-08 10:57:00Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:57:00Client 1 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:57:03New Client Connected

      -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_connect' for 'CONNECT' -

      -- Socket Err: 2018-11-08 10:57:03<-- String?

      -- Socket Err: 2018-11-08 10:57:030@103.92.200.253 --> [SND_EVT&88251&1&08112018&225400&101&478&0&40&0&&]

      -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:57:05<-- [ACK_EVT&88251&]

      -- Socket Err: 2018-11-08 10:57:05<-- String?

      -- Socket Err: 2018-11-08 10:57:05Client 0 from 103.92.200.253 Disconnecting


      Below is my code for tcpListenerService.php



      <?php
      require_once (dirname ( dirname ( __FILE__ ) ) . "/include/config.php");
      require_once ( LIBRARY_PATH. "/SocketServer.class.php"); // Include the File
      date_default_timezone_set('Asia/Kolkata');


      if(!isset($_GET["ip"])||!isset($_GET['port']))
      die("Enter Valid IP & POrt");

      $ip = trim($_GET["ip"]);
      $port=trim($_GET['port']);
      $close = null;





      if(isset($_GET["close"]))
      $close=1;
      $server = new SocketServer($ip,$port,$close); // Create a Server binding to the given ip address and listen to port 31337 for connections
      //$server = new SocketServer("192.168.100.110",1500);
      $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.

      $result = explode("&", $trim);




      //for date and time conversions
      $dt = $result[3];
      $year=substr($dt,4);
      $month=substr($dt,2,2);
      $date=substr($dt,0,2);
      $ty=$result[4];
      $hr=substr($ty,0,2);
      $min=substr($ty,2,2);
      $sec= substr($ty,4,5);


      $date_time = date("Y-m-d", strtotime("$date-$month-$year")) . ' ' . date('H:i:s', strtotime("$hr:$min:$sec"));

      //get the device IP

      $device_ip=$client->{"ip"};


      try {
      // open the connection to the database - $host, $user, $password, $database should already be set
      //connect to the master DB
      $masterconn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD, MASTER_DB_NAME );


      // did it work?
      if (mysqli_connect_errno()) {
      throw new Exception("Failed to connect to MySQL: " . mysqli_connect_error());
      }


      //get the appropriate company mapped for the device IP
      $query=mysqli_query($masterconn,"SELECT c.company_id,d.company_db_name
      FROM company_biometric_devices_ip c
      INNER JOIN company_details d ON c.company_id = d.company_id
      WHERE device_ip ='$device_ip'");

      $devices=array();
      //get the multiple companies for single device
      while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
      $devices = $row;

      // did it has values?
      if (!$devices) {
      throw new Exception("No Company Found for the device [{$device_ip}]");
      }

      //loop and connect it to the company database
      foreach($devices as $device){
      $company_id=$device["company_id"];
      $company_name=DB_PREFIX.$device['company_db_name'];


      //connect with the company DB Insert the data into the biometrics table
      $conn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD,$company_name);
      if (mysqli_connect_errno()) {
      throw new Exception("Failed to connect to Company Db [ $company_name]: " . mysqli_connect_error());
      }


      $stmt=mysqli_prepare($conn, "INSERT INTO employee_biometric (date_time,employee_id,ip,event_seq_no,event_id)
      (SELECT '$date_time',$result[6],'$device_ip','$result[1]','$result[5]' FROM device_users WHERE ref_id='$result[6]');");

      //$stmt = mysqli_prepare ($conn,"INSERT INTO employee_biometric (employee_id,date_time,ip,company_id) VALUES (?,?,?,?)" );
      //mysqli_stmt_bind_param ($stmt, 'ssss',$result[6],$date_time,$client->{"ip"},$company_id);

      if (!$res = mysqli_stmt_execute ( $stmt ))
      throw new Exception("MySQL Error: " . mysqli_error ($conn));
      if($res)
      $summary_insert = "CALL ATTENDANCE_SUMMARY_INSERT($result[6],'{$date_time}')";
      $query=mysqli_query($conn,$summary_insert);
      SocketServer::socket_write_smart($client->socket,"[ACK_EVT&{$result[1]}&]");// Send the Client back the String
      SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
      if(mysqli_stmt_affected_rows($stmt)>0)
      break;
      }
      }catch (Exception $e) {
      SocketServer::log($e->getMessage()."String: $trim"." IP: $device_ip",6);
      }


      }


      My server and device automatically get disconnected, how to rectify the same. I have checked in the particular port also in open condition.










      share|improve this question













      I am using Matrix Cosec BioMetric devices for attendance management system. I have coded to sync biometric punches on realtime basis. But the listener services automatically get stopped.



      The Error Log details are as follows:



      -- Socket Err: 2018-11-08 10:56:30New Client Connected

      -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:30<-- String?

      -- Socket Err: 2018-11-08 10:56:300@103.92.200.253 --> [SND_EVT&91118&1&08112018&225421&101&490&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:30Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:32<-- [ACK_EVT&91118&]

      -- Socket Err: 2018-11-08 10:56:32<-- String?

      -- Socket Err: 2018-11-08 10:56:32Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:46New Client Connected

      -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:46<-- String?

      -- Socket Err: 2018-11-08 10:56:460@103.92.200.253 --> [SND_EVT&88248&1&08112018&225343&101&153&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:46Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:48<-- [ACK_EVT&88248&]

      -- Socket Err: 2018-11-08 10:56:48<-- String?

      -- Socket Err: 2018-11-08 10:56:48Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:53New Client Connected

      -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:53<-- String?

      -- Socket Err: 2018-11-08 10:56:530@103.92.200.253 --> [SND_EVT&88249&1&08112018&225350&101&148&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:53Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:54<-- [ACK_EVT&88249&]

      -- Socket Err: 2018-11-08 10:56:54<-- String?

      -- Socket Err: 2018-11-08 10:56:54Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:56:56New Client Connected

      -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:56<-- String?

      -- Socket Err: 2018-11-08 10:56:560@103.92.200.253 --> [SND_EVT&88250&1&08112018&225353&101&132&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:56Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:56:58<-- [ACK_EVT&88250&]

      -- Socket Err: 2018-11-08 10:56:58<-- String?

      -- Socket Err: 2018-11-08 10:56:58New Client Connected

      -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_connect' for 'CONNECT'

      -- Socket Err: 2018-11-08 10:56:58<-- String?

      -- Socket Err: 2018-11-08 10:56:581@103.92.200.253 --> [SND_EVT&68845&1&08112018&225351&101&430&0&40&0&&]

      -- Socket Err: 2018-11-08 10:56:58Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:57:00<-- [ACK_EVT&68845&]

      -- Socket Err: 2018-11-08 10:57:00<-- String?

      -- Socket Err: 2018-11-08 10:57:00Client 0 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:57:00Client 1 from 103.92.200.253 Disconnecting

      -- Socket Err: 2018-11-08 10:57:03New Client Connected

      -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_connect' for 'CONNECT' -

      -- Socket Err: 2018-11-08 10:57:03<-- String?

      -- Socket Err: 2018-11-08 10:57:030@103.92.200.253 --> [SND_EVT&88251&1&08112018&225400&101&478&0&40&0&&]

      -- Socket Err: 2018-11-08 10:57:03Triggering Hook 'handle_input' for 'INPUT'

      -- Socket Err: 2018-11-08 10:57:05<-- [ACK_EVT&88251&]

      -- Socket Err: 2018-11-08 10:57:05<-- String?

      -- Socket Err: 2018-11-08 10:57:05Client 0 from 103.92.200.253 Disconnecting


      Below is my code for tcpListenerService.php



      <?php
      require_once (dirname ( dirname ( __FILE__ ) ) . "/include/config.php");
      require_once ( LIBRARY_PATH. "/SocketServer.class.php"); // Include the File
      date_default_timezone_set('Asia/Kolkata');


      if(!isset($_GET["ip"])||!isset($_GET['port']))
      die("Enter Valid IP & POrt");

      $ip = trim($_GET["ip"]);
      $port=trim($_GET['port']);
      $close = null;





      if(isset($_GET["close"]))
      $close=1;
      $server = new SocketServer($ip,$port,$close); // Create a Server binding to the given ip address and listen to port 31337 for connections
      //$server = new SocketServer("192.168.100.110",1500);
      $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.

      $result = explode("&", $trim);




      //for date and time conversions
      $dt = $result[3];
      $year=substr($dt,4);
      $month=substr($dt,2,2);
      $date=substr($dt,0,2);
      $ty=$result[4];
      $hr=substr($ty,0,2);
      $min=substr($ty,2,2);
      $sec= substr($ty,4,5);


      $date_time = date("Y-m-d", strtotime("$date-$month-$year")) . ' ' . date('H:i:s', strtotime("$hr:$min:$sec"));

      //get the device IP

      $device_ip=$client->{"ip"};


      try {
      // open the connection to the database - $host, $user, $password, $database should already be set
      //connect to the master DB
      $masterconn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD, MASTER_DB_NAME );


      // did it work?
      if (mysqli_connect_errno()) {
      throw new Exception("Failed to connect to MySQL: " . mysqli_connect_error());
      }


      //get the appropriate company mapped for the device IP
      $query=mysqli_query($masterconn,"SELECT c.company_id,d.company_db_name
      FROM company_biometric_devices_ip c
      INNER JOIN company_details d ON c.company_id = d.company_id
      WHERE device_ip ='$device_ip'");

      $devices=array();
      //get the multiple companies for single device
      while($row = mysqli_fetch_array($query,MYSQLI_ASSOC))
      $devices = $row;

      // did it has values?
      if (!$devices) {
      throw new Exception("No Company Found for the device [{$device_ip}]");
      }

      //loop and connect it to the company database
      foreach($devices as $device){
      $company_id=$device["company_id"];
      $company_name=DB_PREFIX.$device['company_db_name'];


      //connect with the company DB Insert the data into the biometrics table
      $conn = mysqli_connect ( MASTER_DB_HOST, MASTER_DB_USER, MASTER_DB_PASSWORD,$company_name);
      if (mysqli_connect_errno()) {
      throw new Exception("Failed to connect to Company Db [ $company_name]: " . mysqli_connect_error());
      }


      $stmt=mysqli_prepare($conn, "INSERT INTO employee_biometric (date_time,employee_id,ip,event_seq_no,event_id)
      (SELECT '$date_time',$result[6],'$device_ip','$result[1]','$result[5]' FROM device_users WHERE ref_id='$result[6]');");

      //$stmt = mysqli_prepare ($conn,"INSERT INTO employee_biometric (employee_id,date_time,ip,company_id) VALUES (?,?,?,?)" );
      //mysqli_stmt_bind_param ($stmt, 'ssss',$result[6],$date_time,$client->{"ip"},$company_id);

      if (!$res = mysqli_stmt_execute ( $stmt ))
      throw new Exception("MySQL Error: " . mysqli_error ($conn));
      if($res)
      $summary_insert = "CALL ATTENDANCE_SUMMARY_INSERT($result[6],'{$date_time}')";
      $query=mysqli_query($conn,$summary_insert);
      SocketServer::socket_write_smart($client->socket,"[ACK_EVT&{$result[1]}&]");// Send the Client back the String
      SocketServer::socket_write_smart($client->socket,"String? ",""); // Request Another String
      if(mysqli_stmt_affected_rows($stmt)>0)
      break;
      }
      }catch (Exception $e) {
      SocketServer::log($e->getMessage()."String: $trim"." IP: $device_ip",6);
      }


      }


      My server and device automatically get disconnected, how to rectify the same. I have checked in the particular port also in open condition.







      php sockets tcplistener






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 7:07









      Antony Adaikalam

      61




      61





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221226%2fbiometric-fingerprint-sync-socket-error-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221226%2fbiometric-fingerprint-sync-socket-error-php%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check