使用PHP套接字向多个客户端发送和接收数据

时间:2016-08-10 11:45:39

标签: php sockets php-socket

我正在使用SocketServer.class.php从我的服务器接收数据并从远程客户端发送数据。它完美地适用于一个客户端。 客户端是数字仪表,使用连接到它的GPRS调制解调器通过TCP / IP发送数据。每个仪表使用其仪表ID进行区分,其范围为1到247,数据以ASCII格式传递和接收。 PHP代码在Windows命令shell中运行,并将输入读数保存到数据库。 现在,当尝试使用ID为1和2的两米进行相同操作时,它不起作用。我不知道该怎么办。我附上了我迄今为止所执行的代码。

    <?php

require_once("SocketServer.class.php"); // Include the File
    $server = new SocketServer("192.168.1.5",5001); // Create a Server binding to the given ip address and listen to port 5001for connections
    $server->max_clients = 247; // Allow no more than 247 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

/*  //main loop
for($i=1; $i<=247; $i++) {  




    }*/
$server->infinite_loop(); // Run Server Code Until Process is terminated.*/

/*$server   ->  loop_once();*/


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.

        saveInput($input); // this function would save the values to database as it is received
        $output =   "65030063002F"; //65 -> Meter ID 101 in HEX 03 -> function 0063 -> start register 002F -> Number of registers
        $hexad  =   hexToStr($output);
        $hexad  .=  crc16($hexad);
        echo $hexad;
        SocketServer::socket_write_smart($client->socket,&$hexad,"");

}
function saveInput($input)
{
        $res = strToHex($input);
        for ($i=0; $i < strlen($res)-1; $i+=2)
        {
            $string[] = $res[$i].$res[$i+1];
        }
        if(hexdec($string[0]) > 0 && hexdec($string[0]) < 248) {
        echo "Meter --> ".hexdec($string[0])."<br>";
        echo "Function --> ".hexdec($string[1])."<br>";
        $byte = hexdec($string[2]);
        echo "Byte Count --> ".$byte."<br>";
/*      $l=40100; 
            for($k=3; $k<($byte+3); $k+=2) {

        echo "Register $l --> ".hexdec($string[$k].$string[$k+1])."<br>";
        $l++;
        }*/
        echo "<br>";
        }
}
function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= str_pad(dechex(ord($string[$i])),2,"0",STR_PAD_LEFT);

    }
    $hex = trim($hex);
    return $hex;
}
 function crc16($data)
 {
   $crc = 0xFFFF;
   for ($i = 0; $i < strlen($data); $i++)
   {
     $crc ^=ord($data[$i]);

        for ($j = 8; $j !=0; $j--)
        {
            if (($crc & 0x0001) !=0)
            {
                $crc >>= 1;
                $crc ^= 0xA001;
            }
            else
                $crc >>= 1;
        }
    }   
    $crc = dechex($crc);
    $order = str_split($crc, 2);
    $order = array_reverse($order);
    $crc    =   implode($order);
    $crc    =   hexToStr($crc);
   return $crc;
 }
?>

返回值全部以十六进制存储。有没有更好的方法来做到这一点。

我想将65030063002FFC2C作为输入计量10166030063002FFC1F作为输入计数,标识为“102”,依此类推。只有当仪表ID在仪表输入处获得匹配时,它才会将相应的读数返回给服务器。我这么多天都试图解决这个问题,但由于我是PHP套接字的新手并且知识很少,所以很难。

1 个答案:

答案 0 :(得分:1)

所以既然没有人回答,我做了一些挖掘并绕过它。但它包括一个循环。所以有几米。我可以使用套接字写入和循环将数据发送到仪表。没有使上述代码工作的部分是在发送100米的请求数据包时,它实际上接收前8个字节并拒绝其余的。所以我添加了一个延迟睡眠(1),代码就像魅力一样

相关问题