C# PHP-CGI - 调用 phpinfo 使网络服务器崩溃

发布于 2024-12-25 10:37:13 字数 920 浏览 2 评论 0原文

我创建了一个简单且小型的网络服务器,仅用于处理 GET 请求。我还想添加 PHP 支持并对其进行管理。但有一个问题:

每次我尝试在 .php 文件中调用 phpinfo() 时,我的服务器都会停在“WaitForExit”进程上。

 class FastPHP
    {
        private string _phpPath = @"C:\\Program Files (x86)\\PHP\\php-cgi.exe";
        Process p;

        public FastPHP(string filename)
        {
            p = new Process();
            p.StartInfo.FileName = this._phpPath;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = "-q \""+filename+"\"";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
        }

        public string getPHPOutput()
        {
            p.Start();
            p.WaitForExit();
            string sOutput = p.StandardOutput.ReadToEnd();
            p.Close();

            return sOutput;
        }
    }

我的 PHP.ini 设置应该没问题,我调整了所有内容以供 fastcgi 使用。有什么想法如何解决这个问题吗?

I created a simple and small webserver for only handling GET requests. I also wanted to add PHP support and also managed it. But there is one problem:

Everytime I try to call phpinfo() inside a .php file my server stops at "WaitForExit" Process.

 class FastPHP
    {
        private string _phpPath = @"C:\\Program Files (x86)\\PHP\\php-cgi.exe";
        Process p;

        public FastPHP(string filename)
        {
            p = new Process();
            p.StartInfo.FileName = this._phpPath;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.Arguments = "-q \""+filename+"\"";
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.UseShellExecute = false;
        }

        public string getPHPOutput()
        {
            p.Start();
            p.WaitForExit();
            string sOutput = p.StandardOutput.ReadToEnd();
            p.Close();

            return sOutput;
        }
    }

my PHP.ini settings should be fine, I adapted everything for fastcgi use. Any Ideas how to fix this problem?

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

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

发布评论

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

评论(1

×眷恋的温暖 2025-01-01 10:37:13

问题是 StandardOutput 具有一定的缓冲区大小。如果此缓冲区已满,则任何对 stdout 的 write() 都会阻塞。现在,如果您调用p.WaitForExit(),您将无限期地等待。

解决方案是首先从 StandardOutput 读取所有内容,然后调用 WaitForExit

The problem is that the StandardOutput has a certain buffer size. If this buffer is full then any write() to stdout will block. Now if you call p.WaitForExit() you are waiting indefinitely.

The solution is to first read everything from the StandardOutput and then call WaitForExit.

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