title: Writing a program with remote shell functionality like netcat
body: <p>I am interested in writing a program which can do something like netcats "nc -L -d -p  -t -e cmd.exe" command. So it provides a remote shell that is. I have tried piping output and input from and to cmd.exe and sending and receiving it over a socket but it doesn't really seem to work well. Are there any other ways to do it? I am programming in C# by the way.</p>
  
  <p>This is some test code I wrote to test if I could make my own "shell". The output of this is what should be sent over a socket. The program, however, halts when it becomes time to read the output. This is only remedied by using the .readline() method, but I dont know how to detect when it should not read anymore lines.</p>
  
  <pre><code>        Process p = new Process();
          p.StartInfo.FileName = "cmd.exe";
          p.StartInfo.UseShellExecute = false;
          p.StartInfo.RedirectStandardOutput = true;
          p.StartInfo.RedirectStandardInput = true;
          p.Start();
          StreamReader sr = p.StandardOutput;
          StreamWriter sw = p.StandardInput;
  
          while (true)
          {
              Console.Write("&gt;&gt; ");
              string cmd = Console.ReadLine();
              sw.WriteLine(cmd);
              var resp = sr.ReadLine(); 
              Console.WriteLine(resp);
          }
  </code></pre>
  
  <p>Thanks.</p>