PHP 的 fwrite 停止写入套接字文件指针

发布于 2024-09-10 19:01:28 字数 1174 浏览 2 评论 0原文

我有以下 PHP 代码,用于写入使用 fsockopen 打开的文件指针 $fp

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");

$bytes = 0;
while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $bytes))))
{
    syslog(LOG_INFO, "  - " . $w . " bytes written to socket");
    $bytes += $w;
}

if ($bytes != strlen($buf))
{
    syslog(LOG_INFO, "error while writing to socket");
    exit();
}

只要 $buf 的大小,此代码就可以正常工作code> 足够小。大量数据无法完整写入。我得到以下输出:

Write 4900360 bytes to socket:
  - 11096 bytes written to socket
error while writing to socket

顺便说​​一句。 fwrite 的返回值为 0 而不是 false

有人知道可能是什么问题吗?非常感谢您的回答,

当删除 fwrite 前面的 @ 时,我收到以下通知:

Notice: fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer in /root/test.php on line 10

Notice: fwrite(): send of 8192 bytes failed with errno=32 Broken pipe in /root/test.php on line 10

我刚刚嗅探了 TCP 流,我发现,我得到了一个

HTTP/1.1 413 Request Entity Too Large

这个问题有解决办法吗?我使用 lighttpd/1.4.22 服务器

I have the following PHP code for writing to a filepointer $fp that was opened using fsockopen:

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");

$bytes = 0;
while ($bytes < strlen($buf) && ($w = @fwrite($fp, substr($buf, $bytes))))
{
    syslog(LOG_INFO, "  - " . $w . " bytes written to socket");
    $bytes += $w;
}

if ($bytes != strlen($buf))
{
    syslog(LOG_INFO, "error while writing to socket");
    exit();
}

This code works fine as long as the size of $buf is small enough. A large amount of data cannot be written completely. I get the following output:

Write 4900360 bytes to socket:
  - 11096 bytes written to socket
error while writing to socket

btw. the return value of fwrite is 0 and not false.

Does anybody has an idea what could be the problem? Thanks a lot for your answers

I get the following notices when removing the @ in front of the fwrite:

Notice: fwrite(): send of 8192 bytes failed with errno=104 Connection reset by peer in /root/test.php on line 10

Notice: fwrite(): send of 8192 bytes failed with errno=32 Broken pipe in /root/test.php on line 10

I just sniffed the TCP Stream and I figured out, that I get a

HTTP/1.1 413 Request Entity Too Large

Is there any fix to this problem? I use a lighttpd/1.4.22 server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-09-17 19:01:28

试试这个,

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");

$bytes = 0;
do
{
    $w = fwrite($fp, substr($buf, $bytes));
    $bytes += $w;
            syslog(LOG_INFO, "written: ".$w);
    if ($w === 0)
    {
        // end of write
        break;
    }
    if ($w === false)
    {
             syslog(LOG_INFO, "error while writing to socket");
             exit();
    }
} while (true);

我相信你的 while 循环是之前的问题,没有像假设的那样返回 true 等等。

看看它何时停止写入

Try this

syslog(LOG_INFO, "Write " . strlen($buf) . " bytes to socket:");

$bytes = 0;
do
{
    $w = fwrite($fp, substr($buf, $bytes));
    $bytes += $w;
            syslog(LOG_INFO, "written: ".$w);
    if ($w === 0)
    {
        // end of write
        break;
    }
    if ($w === false)
    {
             syslog(LOG_INFO, "error while writing to socket");
             exit();
    }
} while (true);

I believe your while loop was the problem previously, wasn't returning true like it was suppose too etc..

See when it stops writting

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文