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

查看:356
本文介绍了在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 \n");
    }

    echo "Socket created \n";

    // 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 \n");
    }

    echo "Socket bind OK \n";

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

        //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的解压缩.

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

在不知道要返回的数据的情况下,很难确切知道提供包装的格式.看起来解压缩至少可以返回一个十进制值数组(假设您要返回字符),然后可以使用

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'更新了格式字符串以匹配无限数量的字符.net/pack"rel =" nofollow>此处.

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天全站免登陆