dongmi1995 2019-05-24 09:32
浏览 158

解析PHP socket_listener上的套接字数据

I was able to open a PHP TCP listener socket, but I don't know how to parse the buffer. The client that connects to my socket send a text/html with an additional boundary data with a xml file and an image file.

How can I parse the response to get the XML file on one side and the Image on the other side?

server = socket_create_listen(8086);
socket_getsockname($server, $addr, $port);
if (!$server) {
    $message = 'Start TCP socket: Ko. Could not create socket.';
    $this->logger->info($message);
    die($message);
} else {
    $message = 'Start TCP socket: Ok. TCP socket opened on: ' . $addr . ':' . $port . '.';
    $this->logger->info($message);

    while ($c = socket_accept($server)) {
        socket_getpeername($c, $raddr, $rport);
        $this->logger->info("Received Connection from $raddr:$rport
");

        $data = '';
        while ($bytes = socket_recv($c, $r_data, 1024, MSG_WAITALL)) {
            $data .= $r_data;
        }

        //Edited: Explode with double line and got data
        $parsedData = explode("

", $data);
        $xml = new \SimpleXMLElement($parsedData[2]);

        print_r($xml);

        else {
            echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($c)) . "
";
        }

        socket_close($c);
    }
}
fclose($server);

This is the output I received:

Received Connection from :59048

Read 3096 bytes from socket_recv(). Closing socket...POST /test HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-CN
Content-Type: multipart/form-data;boundary=-------------------------7e13971310878
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: 0.0.0.0:7200
Content-Length: 516032
Connection: Keep-Alive
Cache-Control: no-cache

---------------------------7e13971310878
Content-Disposition: form-data; name="file.xml";filename="file.xml";
Content-Type: text/xml
Content-Length: 2273

<EventNotificationAlert version="2.0" xmlns="http://www.isapi.org/ver20/XMLSchema">
<ipAddress></ipAddress>
<ipv6Address></ipv6Address>
<portNo></portNo>
<!--REST OF XML DATA-->
</EventNotificationAlert>

---------------------------7e13971310878
Content-Disposition: form-data;name="image.jpg";filename="image.jpg";
Content-Type: image/pjpeg
Content-Length: 7164

����JFIF���



       !"$"^C

EDITED: I was able to got the XML data by using "explode" function, but I don't know how to get the 2 images as image files. Any suggestion?

Any help would be really appreciated!

Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c