title: C# sending a command over TCP and getting a json back
body: <p>Basically I am writing a small windows application to send and I am not sure how to send a command over a TCP socket to a local network machine. All the examples I looked up in google look way to complicated for what I am looking to do. so far I have this in my c# program </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Net;
  
  namespace MiningMonitorClientW
  {
  static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      [STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new form1());
          string userworker = Textbox1 + TextBox2;
      }
  
  
      public static string Textbox1 { get; set; }
  
      public static string TextBox2 { get; set; }
   }
  }
  </code></pre>
  
  <p>basically I'm trying to emulate this code I have in ruby below in C#</p>
  
  <p>sends a command over TCPsocket to the given IP then saves the response which is json.</p>
  
  <pre><code>s = TCPSocket.new '192.xxx.x.x', xxxx
  s.puts '{"command": "devs"}'
  dev_query = s.gets
  </code></pre>
  
  <p>then sends up to a website using http put request</p>
  
  <pre><code>path = "/workers/update"
  host = "https://miningmonitor.herokuapp.com"
  puts RestClient.put "#{host}#{path}", updateinfo, {:content_type =&gt; :json} 
  </code></pre>
  
  <p>I would love to this in c# someone help me!!!1 :D
  Thanks!</p>
  
  <p>Well I ended up solving my problem and the code below sends a string over TCP and then receives a json back in a string.</p>
  
  <pre><code> byte[] bytes = new byte[1024];
          try
          {
              IPAddress ipAddr = IPAddress.Parse("xxx.xxx.x.xxx");
              IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 4028);
  
              Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
              sender.Connect(ipEndPoint);
              Console.WriteLine("Socket connected to {0}", sender.RemoteEndPoint.ToString());
  
              string SummaryMessage = "string to send";
              byte[] msg = Encoding.ASCII.GetBytes(SummaryMessage);
  
              sender.Send(msg);
              byte[] buffer = new byte[1024];
              int lengthOfReturnedBuffer = sender.Receive(buffer);
              char[] chars = new char[lengthOfReturnedBuffer];
  
              Decoder d = System.Text.Encoding.UTF8.GetDecoder();
              int charLen = d.GetChars(buffer, 0, lengthOfReturnedBuffer, chars, 0);
              String returnedJson = new String(chars);
              Console.WriteLine("The Json:{0}", returnedJson);
              sender.Shutdown(SocketShutdown.Both);
              sender.Close();        
          }
          catch (Exception ex)
          {
              Console.WriteLine("Exception: {0}", ex.ToString());
          }
  </code></pre>