3

I am having some trouble using the redirected input/output of a process. Originally, I had two applications communicating over tcp/ip. The server tells the client to open up cmd.exe and then gives commands to the client that the client has to redirect to the cmd.exe process. Then the client reads the output and sends it back to the server. Basically I was creating a way to use the command line remotely.

The problem is it that it works for the first command and then nothing afterwards. I was able to recreate the problem without using tcp/ip.

Process p = new Process();
ProcessStartInfo psI = new ProcessStartInfo("cmd");
psI.UseShellExecute = false;
psI.RedirectStandardInput = true;
psI.RedirectStandardOutput = true;
psI.CreateNoWindow = true;
p.StartInfo = psI;
p.Start();
p.StandardInput.AutoFlush = true;
p.StandardInput.WriteLine("dir");
char[] buffer = new char[10000];
int read = 0;
// Wait for process to write to output stream
Thread.Sleep(500);
while (p.StandardOutput.Peek() > 0)
{
    read += p.StandardOutput.Read(buffer, read, 10000);
}
Console.WriteLine(new string(buffer).Remove(read));

Console.WriteLine("--- Second Output ---");
p.StandardInput.WriteLine("dir");
buffer = new char[10000];
read = 0;
Thread.Sleep(500);
while (p.StandardOutput.Peek() > 0)
{
    read += p.StandardOutput.Read(buffer, read, 10000);
}
Console.WriteLine(new string(buffer).Remove(read));
Console.ReadLine();

This is obviously ugly test code, but I get the same results. I can read the output the first time and then it comes empty the second time around. I am guessing when I use the output stream the first time around I am locking it and preventing cmd.exe to use that stream again? If that is true what is the correct way of using the output stream multiple times after each input command. I would like to do this synchronously to maintain the feeling of the command line. If the only solution is to read the output stream asynchronously is there a way I could figure out generically when the process finished executing my input? I dont want the server to tell the client to execute another command before the first one finishes.

Thanks.

4
  • When I need this I install cygwin sshd on the server and use paramiko in the python client. Commented Nov 7, 2010 at 4:02
  • I forget who wrote them, but there's also the command line to socket adapter programs called, I think, faucet and hose. They're written in C and part of a package called netpipes, which can be found here: purplefrog.com/~thoth/netpipes/netpipes.html Commented Nov 7, 2010 at 4:18
  • p.StandardOutput.Peek() returns the next char, not the number of characters to read. Your last argument to StandardOutput.Read should be a constant or 10000 - read. Commented Nov 7, 2010 at 6:11
  • Wow thanks, I really misread that description. Fortunately, it really doesn't break anything. Commented Nov 7, 2010 at 6:25

1 Answer 1

6

Does it have to be the same cmd session for all the commands? how about:

    private static void RunCommand(string command)
    {
        var process = new Process()
                          {
                              StartInfo = new ProcessStartInfo("cmd")
                               {
                               UseShellExecute = false,
                               RedirectStandardInput = true,
                               RedirectStandardOutput = true,
                               CreateNoWindow = true,
                               Arguments = String.Format("/c \"{0}\"", command),
                               }
                          };
        process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
        process.Start();
        process.BeginOutputReadLine();

        process.WaitForExit();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

I guess if I am just going to use the console to browse and maybe kick off some programs this would work. I would have to manually keep track of the working directory but this could be a decent temporary solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.