title: socat TCP listener with .NET Client
body: <p>I have a socat TCP listener running on Linux (SLES 12). Any client that connects will pass a string to the socket and socat will execute a script which does some work with based on the string. The script echos some output which is passed back to client.</p>
  
  <pre><code>socat  TCP-LISTEN:9996,fork EXEC:/home/abhishek/hello.sh
  </code></pre>
  
  <p>Below is the hello.sh script.</p>
  
  <pre><code>#!/bin/bash
  read str
  echo "[Hello] $str" | tee -a test.txt
  </code></pre>
  
  <p>When I run ncat client this works as expected. ncat is able to get the data back and output it.</p>
  
  <pre><code>echo abhishek | ncat 192.168.1.12 9996
  [Hello] abhishek
  </code></pre>
  
  <p>Now I want to connect to socat through a .NET client written in C#. Below is the raw socket based code I have come up with. </p>
  
  <pre><code>IPAddress address = IPAddress.Parse("192.168.1.12");
  IPEndPoint ipe = new IPEndPoint(address, 9996);
  
  Socket client = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  
  client.Connect(ipe);
  
  
  byte[] msg = Encoding.ASCII.GetBytes("abhishek");
  
  // Send the data through the socket.  
  int bytesSent = client.Send(msg);
  Console.WriteLine("Sent {0} bytes.", bytesSent);
  
  byte[] bytes = new byte[128];
  int bytesRec = client.Receive(bytes); ;
  Console.WriteLine("Response text = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
  
  client.Shutdown(SocketShutdown.Both);
  client.Close();
  </code></pre>
  
  <p>I get the line "Sent 8 bytes" but after that client hangs on Receive. The client data is received by hello.sh becaused test.txt contains new entry. When I kill the client (Ctrl+C) socat prints </p>
  
  <pre><code>2018/02/03 10:12:03 socat[21656] E write(4, 0xe530f0, 17): Broken pipe
  </code></pre>
  
  <p>How should I read the reply from socat in C#?</p>
  
  <p>Thanks</p>