0

I am using iperf-2.0.5-2-win32 tool to find network bandwidth. I have written codes in c# which opens the cmd prompt, pass iperf parameters to start server side & client side. iperf-2.0.5-2-win32 exe will not open directly, need to open through cmd prompt only. At present the output(Transfer rate & Bandwidth) is displaying on cmd prompt itself. I want these output to be displayed in textbox I have tried StreamReader also. But it takes null, I have also tried OutputDataReceived Event, its also taking null. Found few codes for ipconfig & ping.but those were not working with iperf codes.

button_click event(),
{
Process Client_proc = new Process();
ProcessStartInfo Client_command = new ProcessStartInfo("cmd.exe"); 
string ip = txtIP.Text;
Client_command.CreateNoWindow = true;
Client_command.WindowStyle = ProcessWindowStyle.Hidden;
Client_command.WorkingDirectory = @"E:\Iperf\RunEXE_Through_Application\iperf-2.0.5-2-win32";
Client_command.Arguments = "/c START iperf -c " + ip;
Client_proc.StartInfo = Client_command;
Client_command.RedirectStandardOutput = true;
Client_command.UseShellExecute = false;
Client_proc.OutputDataReceived += new DataReceivedEventHandler(Client_proc_OutputDataReceived);
Client_proc.Start(); 
Client_proc.BeginOutputReadLine(); 
Client_proc.WaitForExit();
}

void Client_proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string newLine = e.Data.Trim() + Environment.NewLine;
MethodInvoker append = () => txtOutput.Text += newLine;
txtOutput.BeginInvoke(append);
}
}

Plz help me.Earlier responses are appreciated Thanks

  • What exactly do you mean by "it takes null"? – hschne May 21 '14 at 12:55
  • @hSchroedl- if i keep break point and check value of E.Data() is null" if i keep break point and check value of e.Data() in Client_proc_OutputDataReceived is null – user1244453 May 22 '14 at 04:51
  • Hmm. I tried iperf myself, but I can't get an output even if I lauch it from from a cmd window... so debugging this is kinda difficult for me. I recommend you execute it from the command line, like you normally would, and see what output that gives you. One other possible problem I noticed is that "START" will open another window/process, which you would not have any listeners/streamreaders attached to. Thats all I have, sorry. – hschne May 22 '14 at 09:08

2 Answers2

1

you use this complete code for your disposal It is not perfect (some problems when using multiple streams )

public void RunProcess(string FileName, string Arguments, bool EventWhenExit )
{
    process = new Process();

    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.LoadUserProfile = false;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
    process.StartInfo.Arguments = Arguments;//Gets or sets the set of command-line arguments to use when starting the application      
    Thread.Sleep(1000);
    if (EventWhenExit)
    {
        process.EnableRaisingEvents = true;

        process.Exited += new EventHandler(myprocess_Exited);/*New line */

    }

    process.Start();
    process.BeginOutputReadLine();
    PID = process.Id;


}

private void myprocess_Exited(object sender, EventArgs e)
{
    process.Refresh();
    Thread.Sleep(2000);
    onProcessEnd(this, "ENDOF " + Proc.ToString());
    Console.WriteLine("Process exsiting ");
}


private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{

    string OutputFromProcess = e.Data;
    //fire event to event handler class for further use
    onDataOutputFromProcess(this, OutputFromProcess, Proc.ToString());
}

than in your GUI layer you should bind to onDataOutputFromProcess event there you should have something like

if (screenToPrint.InvokeRequired) //&& this.Visible)
{
    try
    {
        this.Invoke(new Action<AppendToScreenParam>(AppendTextFullConfig), new object[] { append });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    return;
}
else
{
    screenToPrint.SelectionFont = font;
    screenToPrint.SelectionColor = append.Color;
    //screenToPrint.AppendText(append.Message);
    string TextToPrint = string.Format("{0}\n", append.Message);
    screenToPrint.AppendText(TextToPrint);
}

}

LordTitiKaka
  • 2,087
  • 2
  • 31
  • 51
0

Maybe it is because iperf process is returning error. Subscribe the ErrorDataReceived event with Client_proc.ErrorDataReceived += Client_proc_ErrorDataReceived; and see the results. If command returns error, you can see the error message as output.

void Client_proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        this.txtOutput.BeginInvoke(new MethodInvoker(() => { this.txtOutput.Text = e.Data; }));
    }
}
CuriousPen
  • 249
  • 1
  • 8
  • @CuriousPen-No CuriousPen, I get the output in the same command prompt,I dont get any error message.I wish to redirect the command prompt output to a textbox. I am stuck in this part. Could u plz look into this – user1244453 May 22 '14 at 04:52
  • @user1244453 What is strange is I can get all the command line output from ErrorDataReceivent event. You can try with -v or --help parameters with iperf command for example. All output is written to ErrorStream – CuriousPen May 22 '14 at 07:16