dongmaopiao0901 2015-09-23 15:53
浏览 56

使用相同连接时,套接字请求在第二次调用时失败

Im opening up a connection to a server that accepts XML requests. I am required to send two requests one after the other.

If I run the first request on its own I get data back If I run the second request on its own I get data back

If I run both within a loop using the same connection, only the first request works, the second returns no data.

Is there something I need to send to the socket between each request to indecate the end of each request, otherwise what am I doing wrong?

// Open socket connection
$socket = pfsockopen($this->config['ip'], $this->config['port'], $errno, $errstr, 30);

// Try and open a connection
if ( ! $socket) {
    throw new \Exception($errstr . '('.$errno.')');
}

// If connection was successfull start sending requests
else{

    // Loop each request within the container
    foreach($this->container as $key => $object){

        print "Request" . ($key+1) . PHP_EOL;

        // Reset data and post string
        $data = array();
        $xml_post_string = null;

        // Create XML string
        $xml = \LSS\Array2XML::createXML('request', $object->request);
        $xml_post_string = $xml->saveXML();

        // Add ending lines
        $xml_post_string = $xml_post_string . PHP_EOL . PHP_EOL;

        // Loop
        fwrite($socket, $xml_post_string);
        while ( ! feof($socket)) {
            $data[] = fgets($socket, 1024);
        }

        // Tried adding, but fails
        ftruncate($socket, 2);

        if(count($data)){
            print implode($data);
        }

        else{
            print "No response from server - you broke it" . PHP_EOL;
        }
    }
}

fclose($socket);

A second attempt. The results are the same for any random server out there.

        // Create a TCP/IP socket
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

        // Connect the socket
        socket_connect($socket, 'xxx.xxx.xxx.xxx', '80');

        $xml_post_string_one = '<request><message>Hello first world</message></request>';
        $xml_post_string_two = '<request><message>Hello second world</message></request>';

        // FIRST ROUND //

        // Send data to it
        socket_write($socket, $xml_post_string_one, strlen($xml_post_string_one));
        // Get data from it
        socket_recv($socket, $bufA, 2048, MSG_WAITALL);
        // Done
        print strlen($bufA) . PHP_EOL; // Got info back

        // SECOND ROUND //

        // Send data to it
        socket_write($socket, $xml_post_string_two, strlen($xml_post_string_two));
        // Get data from it
        socket_recv($socket, $bufB, 2048, MSG_WAITALL);
        // Done
        print strlen($bufB). PHP_EOL; // Returns 0, Why?
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

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