title: C# Telnet Server Client
body: <p>I am writing a telnet client and server in c#. 
  The problem is that when I give in the command in the client. My server gives the error:
  "<em>The system cannot find the file specified</em>"</p>
  
  <p>My server is in a console app and my client is in a winform.
  Thank you very much.</p>
  
  <p>Here are the codes:
  <strong>Server</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  using System.Diagnostics;
  
  namespace TelnetServerVictim
  {
      class Program
      {
          static void Main(string[] args)
          {
              IPAddress HOST = IPAddress.Parse("127.0.0.1");
              IPEndPoint serverEP = new IPEndPoint(HOST, 443);
              Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              sck.Bind(serverEP);
              sck.Listen(443);
  
              try
              {
                  Console.WriteLine("Listening for clients...");
                  Socket msg = sck.Accept();
  
                  while (true)
                  {
                      // Send a welcome greet
                      byte[] buffer = Encoding.Default.GetBytes("Welcome to the server of Kobernicus!!");
                      msg.Send(buffer, 0, buffer.Length, 0);
                      buffer = new byte[255];
  
                      // Read the sended command
                      int rec = msg.Receive(buffer, 0, buffer.Length, 0);
                      byte[] bufferReaction = Encoding.Default.GetBytes(rec.ToString());
  
                      // Run the command
                      Process prcsCMD = new Process();
                      prcsCMD.StartInfo.FileName = bufferReaction.ToString();
                      prcsCMD.StartInfo.UseShellExecute = false;
                      prcsCMD.StartInfo.Arguments = string.Empty;
                      prcsCMD.StartInfo.RedirectStandardOutput = true;
                      prcsCMD.Start();
  
                      string output = prcsCMD.StandardOutput.ReadToEnd();
                      byte[] cmdOutput = Encoding.Default.GetBytes(output);
                      msg.Send(cmdOutput,0,cmdOutput.Length,0);
                      cmdOutput = new byte[255];
                  }
                  sck.Close();
                  msg.Close();
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
          }
      }
  }
  </code></pre>
  
  <p>CLient:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Net;
  using System.Net.Sockets;
  
  namespace TelnetClientME
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
          Socket sck;
          private void btnConnect_Click(object sender, EventArgs e)
          {
              sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 443);
              try
              {
                  sck.Connect(endPoint);
                  MessageBox.Show("You are connected to the server!");
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
  
          private void btnSendMSG_Click(object sender, EventArgs e)
          {
              byte[] buffer = Encoding.Default.GetBytes(txtCMD.Text);
              sck.Send(buffer, 0, buffer.Length, 0);
              buffer = new byte[255];
              int rec = sck.Receive(buffer, 0, buffer.Length, 0);
  
              txtOutputCMD.Text = rec.ToString();
          }
  
          private void btnClear_Click(object sender, EventArgs e)
          {
              txtOutputCMD.Clear();
          }
  
          private void btnExit_Click(object sender, EventArgs e)
          {
              sck.Close();
              Application.Exit();
          }
      }
  }
  </code></pre>
  
  <p>Thank you very much.
  txtOutputCMD = a richTextBox where the output of the command should be.
  txtCMD = a textbox where you can type in the command.</p>