C# Iperf Server - c#

I'm trying to write a C# wrapper for Iperf server. After Iperf client is done with packet sending, the C# server application should dump the output data to the text file.
The problem is that this process (server) never exits, so it doesn't dump any data to the txt file. However, when I manually close the cmd window that runs iperf server, the text file is written with data (process exits). But this is clearly not the solution I'm looking for.
Any suggestions how can I write the data directly into the file, w/o need of manually closing the iperf server cmd window?
This is my code:
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);
}

Instead of shelling out to a terminal, have you considered using their API? iperf3 introduced "libiperf".
There are example C-programs in their source code tree.

Related

Command won't run in Command Prompt

When users click on a button, I want it to run the logon script(launching from server), but each computer in different servers, so I get the server name. But the netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd"; line is not working as it should be. It should run the logon.cmd on the PC(mapping network drivers, printers, etc), and then the CMD should close.
private void MapNetwork_Click(object sender, EventArgs e)
{
Process sln = new Process();
sln.StartInfo.UseShellExecute = false;
sln.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sln.StartInfo.FileName = "cmd.exe";
sln.StartInfo.Arguments = "/c echo %logonserver%";
sln.StartInfo.RedirectStandardOutput = true;
sln.Start();
string slnres = sln.StandardOutput.ReadToEnd();
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = slnres + #"/c \netlogon\logon.cmd";
netlogon.Start();
}
A couple things:
You don't need to run a command prompt to get an environment variable. You can use Environment.GetEnvironmentVariable.
Your Arguments property for your call to logon.cmd is being constructed into this:
\\myserver/c \netlogon\logon.cmd
When I think you want this:
/c \\myserver\netlogon\logon.cmd
So make sure you put slnres at the right place in your string. Your code should look like this:
private void MapNetwork_Click(object sender, EventArgs e)
{
string slnres = Environment.GetEnvironmentVariable("logonserver");
label1.Text = slnres;
Process netlogon = new Process();
netlogon.StartInfo.UseShellExecute = false;
netlogon.StartInfo.FileName = "cmd.exe";
netlogon.StartInfo.Arguments = "/c " + slnres + #"\netlogon\logon.cmd";
netlogon.Start();
}
i am a little confused about your question and i am not rly sure if i understand you correctly. some time ago i made a program where i had to run few powershell commands, so i made a class for it. redirected to your button it would look like that:
(and remember you need the fqdn to your file location => Reading File From Network Location)
using System.Diagnostics;
//class lvl scope vars
string output;
string ErrorOutput;
private void MapNetwork_Click(object sender, EventArgs e)
{
//define process arguments
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"cmd.exe";
startInfo.Arguments = #"FQDN path to your file on the server; exit";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
//start process
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
//outpunt handling
if (string.IsNullOrEmpty(ErrorOutput))
{
return output;
}
else
{
return ErrorOutput;
}
}
first of all i would check if your application is able to open the file one the shared network location. (server available? access rights to server? serer mapped?)
after that you can check if he is able to start the file locally. (does it need admin rights to run the *.cmd, *.bat file)
now you can check if your application runs it correctly.

Get real-time CMD command output in C# application

I want to create a desktop application like command prompt. in that application, I get input from a textbox and put the real-time output on rich-textbox.
but some of commands need to wait for complete execute like "composer install" (that will downloading required resources that's why taking long time and it's also displaying process status of downloading in percentage)
So my main question is how can i do something like command prompt that can give dynamic output with light weight no hanging if any other language instead of c# can do that then suggest me.
my desktop application look like this.
and c# "Run" button on click event method is as following.
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
if (textBox1.Text != null)
{
ProcessStartInfo info = new ProcessStartInfo("cmd", "/c " + textBox1.Text);
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.CreateNoWindow = true;
StringBuilder outputBuilder = new StringBuilder();
StringBuilder errorBuilder = new StringBuilder();
Process process = new Process();
process.StartInfo = info;
process.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(SortOutputHandler);
process.Start();
process.BeginErrorReadLine(); // do this after process.Start()
process.BeginOutputReadLine();
process.WaitForExit();
}
}
private void SortOutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
{
StringBuilder sortOutput = new StringBuilder("");
if (richTextBox1.InvokeRequired) { richTextBox1.BeginInvoke(new DataReceivedEventHandler(SortOutputHandler), new[] { sendingProcess, outLine }); }
else
{
sortOutput.Append(Environment.NewLine + outLine.Data);
richTextBox1.AppendText(sortOutput.ToString());
richTextBox1.ScrollToCaret();
}
}
my code is completely works but application is hanging until command is executed completely while run dynamic commands like "composer install"

having trouble running a batch file from my C# windows application

I am trying to run a batch file that runs another batch file from my windows application.
when i click the button, everything is frozen and i do not know why.
The nested batch file was created to build html files from RST files
I have a feeling that I am in a dead lock situation.
Thank you so much guys for your help.
I have attached a screen shot of my form and included my code as well.
private void buttonMakeHtml_Click(object sender, EventArgs e)
{
ProcessStartInfo processInfo =
new ProcessStartInfo("cmd.exe", "/c" + "\"C:\\ReadTheDocs\\makeHtml.bat\"");
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo);
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
textBoxOutPut.Text = output;
textBoxError.Text = error;
}
Make html Screen

Get command line output in real time not working for all cli

The below code is working fine for ping stackoverflow.com and most other cases but when I using 7z.exe it's not real time, it waits until directory is compressed and then shows the output. The argument that I used for compress is a test.7z dirpath. Can I do anything else?
private ProcessStartInfo GetProcessStartInfo(string filename, string arguments)
{
ProcessStartInfo ProcessStartInfo = new ProcessStartInfo();
ProcessStartInfo.CreateNoWindow = true;
ProcessStartInfo.UseShellExecute = false;
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ProcessStartInfo.RedirectStandardOutput = true;
ProcessStartInfo.RedirectStandardError = true;
ProcessStartInfo.RedirectStandardInput = true;
ProcessStartInfo.FileName = filename;
ProcessStartInfo.Arguments = arguments;
}
private void ProcessRun(string filename, string arguments)
{
Process Process = new Process();
Process.StartInfo = GetProcessStartInfo(filename, arguments);
Process.ErrorDataReceived += Process_OutputDataReceived;
Process.OutputDataReceived += Process_OutputDataReceived;
Process.EnableRaisingEvents = true;
Process.Start();
Process.BeginOutputReadLine();
Process.BeginErrorReadLine();
Process.WaitForExit();
}
public ObservableList<string> Output = new ObservableList<string>();
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Output.Add(e.Data);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OutputListBox.ItemsSource = Output;
ProcessRun("cmd.exe", "/c ping stackoverflow.com");
}
7zip Output:
7zip Progress:
If you're interested in the percentage, you're out of luck - that isn't done by standard output. Standard output only handles streams of data, while the percentage output is actually done by manipulating the console directly. It's not part of the output stream - there is no way to replicate the same effect using the standard I/O streams.
So it's not a problem of your code. If you want to see the same problem using just the Command prompt, try running this:
7z.exe yourarguments > log.txt
The > is an output redirect - instead of writing to the console, the standard output is redirected to a file. When you use it with ping, it immediately prints out standard output as it comes. When you use it with 7zip, you get the same result as with your C# application.

Windows Service read cmd lines with eventhandler

I'm trying to run a command in the command prompt and run it as a service. This command starts the queue listener from Laravel. I want to run it as a service so this queue listener always runs in the background. When the listener outputs some lines I want to capture these and send an email. I already tried my code running it as a process and it's working, but when I try to run the code as a service it doesn't start.
Process process();
protected override void OnStart(string[] args)
{
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = "/C php artisan queue:listen --tries=3 --timeout=0 --memory=1024";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.WorkingDirectory = "C:/xampp/htdocs/phpproject";
process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
}
private void p_OutputDataReceived(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the command output.
if (!String.IsNullOrEmpty(outLine.Data.ToString()))
{
sendMail(process.StandardOutput.ToString());
}
}
Edit:
When I comment the process.WaitForExit() line the service runs and the queue listener does its work!.
But the next problem I have is the service never hits the eventhandler. It did when I was running it only as a process. Any clue why this isn't working as a service?
You can try something like this;
private Process process = null;
private DataReceivedEventHandler TheDataReceievedEventHandler;
private void startProcess()
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(#"cmd.exe", #"/C php artisan queue:listen --tries=3 --timeout=0 --memory=1024")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = #"C:/xampp/htdocs/phpproject",
};
if ((process = Process.Start(processStartInfo)) != null)
{
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(ExitedHandler);
TheDataReceievedEventHandler = new DataReceivedEventHandler(StandardOutputHandler);
process.OutputDataReceived += TheDataReceievedEventHandler;
process.BeginOutputReadLine();
process.ErrorDataReceived += TheDataReceievedEventHandler;
process.BeginErrorReadLine();
}
}
private void ExitedHandler(object sender, EventArgs e)
{
throw new NotImplementedException(); // the service you're trying to run closed it self.
}
private void StandardOutputHandler(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}
The problem is if the service you are trying to run closes it self, it will stop outputting and the process will be closed. Similar to CMD behavior each time you send a command to CMD it will close it self after you receive the error or output.
So if for example; I want to use CMD to see my task-list, i will have to build a loop were i run a CMD process on a regular interval because after each command it will close it self.
Edit
If you can't stop the program using its own logic you will need to kill the process of the program itself. You can do that using the following code;
try // If you have no administrator privilege, try will fire.
{
foreach (Process proc in Process.GetProcessesByName("name process")) // You can get the name by looking in your task manager.
{
proc.Kill();
}
}
catch(Exception ex)
{
// Add error handling
}

Categories