在 PHP 中接收 UDP 数据包数据报 [英] Receive UDP packet datagrams in PHP

查看:31
本文介绍了在 PHP 中接收 UDP 数据包数据报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php 构建 GPS 跟踪系统的侦听服务器.GPS 通过 UDP 数据包发送数据,我可以通过运行以下脚本来显示.但是实际数据以符号形式出现,所以我猜我错过了转换

I am working in php to build a listening server for a GPS tracking system. The GPS sends data through UDP packets, which I can display by running the below script. However the actual data comes out in symbols so I am guessing I am missing a conversion

    //Reduce errors
    error_reporting(~E_WARNING);

    //Create a UDP socket
    if(!($sock = socket_create(AF_INET, SOCK_DGRAM, 0)))
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Couldn't create socket: [$errorcode] $errormsg 
");
    }

    echo "Socket created 
";

    // Bind the source address
    if( !socket_bind($sock, "192.168.1.29" , 1731) )
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Could not bind socket : [$errorcode] $errormsg 
");
    }

    echo "Socket bind OK 
";

    //Do some communication, this loop can handle multiple clients
    while(1)
    {
        echo "
 Waiting for data ... 
";

        //Receive some data
        $r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
        echo "$remote_ip : $remote_port -- " . $buf;

            //Send back the data to the client
        //socket_sendto($sock, "OK " . $buf , 100 , 0 , $remote_ip , $remote_port);

    }

    socket_close($sock);

推荐答案

我以前没有用 PHP 做过这个,但我的第一个猜测是你得到一个二进制字符串,你需要将它转换成ASCII(或您使用的任何字符集).

I've not done this with PHP before, but my first guess would be that you are getting a binary string back, which you'll need to convert into ASCII (or whatever character set you're using).

看起来你应该可以使用 PHP 的 unpack 来解决这个问题.

It looks like you should be able to use PHP's unpack for this.

如果不知道要返回什么数据,就很难确切地知道提供包的格式.看起来 unpack 至少能够返回一个十进制值数组(假设您要返回字符),然后您可以使用 chr.可能是这样的:

It's hard to know exactly what format to provide pack without knowing what data you're getting back. It looks like unpack would at least be able to give back an array of decimal values (assuming you're getting characters back), which you could then convert into ASCII, with chr. It might be something like this:

//Receive some data
$r = socket_recvfrom($sock, $buf, 512, 0, $remote_ip, $remote_port);
//Convert to array of decimal values
$array = unpack("c*chars", $buf);
//Convert decimal values to ASCII characters:
$chr_array = array();
for ($i = 0; $i < count($array); $i++)
{
    $chr_array[] = chr($array[$i]);
}

对于二进制数据的解析需要多复杂取决于协议设计(也就是说,您只是发送字符串数据,还是整数和字符串的混合等等......您需要解析二进制数据).

It depends on the protocol design as to how complex your parsing of the binary data needs to be (That is, are you just sending string data, or a mix of integers and strings, etc... You need to parse the binary data accordingly).

我已经更新了格式字符串以匹配不定数量的字符,按照列出的格式使用数组元素名称chars"这里.

I've updated the format string to match an indefinite number of characters, using the array element name 'chars' as per the format listed here.

向代码示例添加了一些基本的 ASCII 转换.

Added some elementary ASCII conversion to code example.

这篇关于在 PHP 中接收 UDP 数据包数据报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆