0

我是 Perl CGI 等方面的初学者。我正在用一段 Perl 代码试验服务器推送概念。它应该每三秒向客户端发送一个 jpeg 图像。

不幸的是,似乎没有任何效果。有人可以帮助确定问题吗?

这是代码:

use strict;
# turn off io buffering
$|=1;
print "Content-type: multipart/x-mixed-replace;";
print "boundary=magicalboundarystring\n\n";
print "--magicalboundarystring\n";

#list the jpg images
my(@file_list) = glob "*.jpg";
my($file) = "";

foreach $file(@file_list ) 
{
     open FILE,">", $file or die "Cannot open file $file: $!";
     print "Content-type: image/jpeg\n\n";

    while ( <FILE> )
    { 
        print "$_";
    }

    close FILE;
     print "\n--magicalboundarystring\n";
     sleep 3;
    next;

}

编辑:添加关闭 i/o 缓冲,添加“使用严格”和“@file_list”,“$file”被设为本地

4

1 回答 1

1

刷新输出。

很可能,服务器将响应保存在缓冲区中。你可能想fflush(STDOUT)在每一次printautoflush STDOUT一次之后做。

看看http://www.abiglime.com/webmaster/articles/cgi/032498.htm

[引用]

要使用下面的脚本,您需要在您的站点上实现一个称为“未解析”的 CGI。通常,Web 服务器会缓冲 CGI 程序的所有输出,直到程序完成。我们不希望这种情况发生在这里。使用 Apache,这很容易。如果您的 CGI 程序的名称以“nph-”开头,它将不会被解析。此外,将 glob "/some/path/*" 更改为您要查找文件的路径。

[/引用]

于 2010-05-20T05:47:58.963 回答