dongxi4235 2014-02-28 19:22
浏览 151

无法捕获PHP套接字连接:: 504网关超时的响应

I am using the following php code to connect and send a request to a remote server through php sockets connection.

<?php 
$host    = "X.X.X.X";
$port    = 1234;
$message = "<request>test</request>";
echo "Message To server :".$message;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket
");
// connect to server
$result = socket_connect($socket, $host, $port) or die("Could not connect to server
");  
// send string to server
socket_write($socket, $message, strlen($message)) or die("Could not send data to server
");
// get server response
stream_set_timeout($socket, 10);
$result = socket_read ($socket, 1024) or die("Could not read server response
");
//echo "Reply From Server  :".$result;
$info = stream_get_meta_data($socket);
if (isset($info['timed_out']) && $info['timed_out'])
{
    echo 'Connection timed out!';
}
else
{
    var_dump($result);
}
// close socket
socket_close($socket);
?>

As confirmed, the remote machine is receiving our request and sending appropriate response to us over the same session. But unfortunately, I am getting the following response when the script is executed -

504 Gateway Time-out. The server didn't respond in time.

When tried from Putty (same server from where the code was executed), I am getting the response almost instantly (in less than a second).

There is no firewall running that would block the responses. All VPN traffic passes through the tunnel without any blocks as seen in telnet request and response.

Now I am unable to figure out the exact reason of not getting the response through this php script. I had gone through many tutorials and sample codes but found no luck. :(

Members, please help.

  • 写回答

3条回答 默认 最新

  • dqz13288 2014-02-28 19:31
    关注

    This might be outdated, but a note on The PHP manual says:

    In case anyone is puzzled, stream_set_timeout DOES NOT work for sockets created with socket_create or socket_accept. Use socket_set_option instead.

    Instead of:

    <?php
    stream_set_timeout($socket,$sec,$usec);
    ?>
    

    Use:

    <?php
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sec, 'usec'=>$usec));
    socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$sec, 'usec'=>$usec));
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码