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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
    • ¥15 C# datagridview 单元格显示进度及值
    • ¥15 thinkphp6配合social login单点登录问题
    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配