2

我正在尝试为 Iperf 服务器编写 C# 包装器。在 Iperf 客户端完成数据包发送后,C# 服务器应用程序应将输出数据转储到文本文件中。问题是这个进程(服务器)永远不会退出,所以它不会将任何数据转储到 txt 文件中。但是,当我手动关闭运行 iperf 服务器的 cmd 窗口时,文本文件会写入数据(进程退出)。但这显然不是我正在寻找的解决方案。任何建议如何将数据直接写入文件,而无需手动关闭 iperf 服务器 cmd 窗口?

这是我的代码:

private void button1_Click(object sender, EventArgs e)
{       
    string commandline_server = " -s -u -i 1";

    try
    {        
        process = new Process();
        process.StartInfo.FileName = "iperf.exe";
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        //process.StartInfo.RedirectStandardError = true;

        process.StartInfo.Arguments = commandline_server;
        process.StartInfo.CreateNoWindow = false;

        process.EnableRaisingEvents = true;
        process.Exited += new EventHandler(process_Exited);

        process.Start();              
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

void process_Exited(object sender, EventArgs e)
{
    //throw new NotImplementedException();
    string outfile = process.StandardOutput.ReadToEnd();
    System.IO.File.WriteAllText("test.txt", outfile);
}
4

1 回答 1

1

你有没有考虑过使用他们的 API,而不是向终端发起攻击?iperf3 引入了“libiperf”。

他们的源代码树中有示例 C 程序。

于 2018-02-23T19:41:00.937 回答