title: Receive and send continuous data from TCP Socket
body: <p>I'm already going crazy. So I decided to ask for help.</p>
  
  <p>Scenario: In a web page, connect to the server via TCP socket, and in a loop I get the data byte by byte. These data are continuous.</p>
  
  <p>Question: How do I make a WebSocket that receive and send data to the player ( html5 tag).</p>
  
  <p>Default.aspx</p>
  
  <pre><code>&lt;asp:Literal ID="ltrPlayer" runat="server" /&gt;
  </code></pre>
  
  <p>Default.aspx.cs</p>
  
  <pre><code>protected void Page_Load(object sender, EventArgs e)
  {
      string tele = HttpContext.Current.Request.QueryString["tele"];
      string ramal = HttpContext.Current.Request.QueryString["ramal"];
      string dac = HttpContext.Current.Request.QueryString["dac"];
      string cti = HttpContext.Current.Request.QueryString["cti"];
  
      if ((tele != null) &amp;&amp; (ramal != null) &amp;&amp; (dac != null) &amp;&amp; (cti != null))
      {
          string strAudio = "&lt;audio controls='controls'&gt;";
          strAudio += "&lt;source src='Play.ashx?tele=" + tele + "&amp;ramal=" + ramal + "&amp;dac=" + dac + "&amp;cti=" + cti + "' type='audio/x-wav' controls preload='auto'&gt;";
          strAudio += "Seu browser não oferece suporte a este player.";
          strAudio += "&lt;/audio&gt;";
  
          ltrPlayer.Text = strAudio;
      }
  }
  </code></pre>
  
  <p>Play.ashx</p>
  
  <pre><code>&lt;%@ WebHandler Language="C#" Class="Play" %&gt;
  
  using System;
  using System.Web;
  
  using System.Net;
  using System.IO;
  using Alvas.Audio; // Make the conversion VOX to WAV
  using System.Media;
  using System.Configuration;
  
  using System.Net.Sockets;
  using System.Text;
  
  
  public class Play : IHttpHandler
  {
      private NetworkStream ns;
  
      public void ProcessRequest(HttpContext context)
      {
          string tele = HttpContext.Current.Request.QueryString["tele"];
          string ramal = HttpContext.Current.Request.QueryString["ramal"];
          string dac = HttpContext.Current.Request.QueryString["dac"];
          string cti = HttpContext.Current.Request.QueryString["cti"];   
  
          if ((tele != null) &amp;&amp; (ramal != null) &amp;&amp; (dac != null) &amp;&amp; (cti != null))
          {
              try
              {
                  TcpClient oClient = new TcpClient();
  
                  oClient.Connect(tele, 22000);
  
                  ns = oClient.GetStream();
                  write(ns, "ondelogar");
  
                  ns = oClient.GetStream();
                  write(ns, "monitorarRamalViaRede(tele;ramal;dac;cti)");
  
                  Thread.Sleep(1000);                
  
                  do
                  {
                      if (oClient.GetStream().DataAvailable == true)
                      {
                          using (StringReader reader = new StringReader(read(ns)))
                          {
                              string line;
  
                              while ((line = reader.ReadLine()) != null)
                              {
                                  Byte[] byteOut = new Byte[line.Length / 2];
  
                                  int i = 0;
                                  while (i &lt; (line.Length / 2))
                                  {
                                      byteOut[i / 2] = Convert.ToByte(line.Substring(i, 2), 16);
                                      i = i + 2;
                                  }
  
                                  Stream s = new MemoryStream(byteOut);
                                  BinaryReader br = new BinaryReader(s);
                                  MemoryStream pcmStream = new MemoryStream();
                                  IntPtr pcmFormat = AudioCompressionManager.GetPcmFormat(1, 16, 6000);
                                  WaveWriter ww = new WaveWriter(pcmStream, AudioCompressionManager.FormatBytes(pcmFormat));
                                  Vox.Vox2Wav(br, ww);
                                  WaveReader wr = new WaveReader(pcmStream);
  
                                  byte[] pcmData = pcmStream.ToArray();
  
                                  br.Close();
                                  ww.Close();
                                  wr.Close();
                                  pcmStream.Close();
                                  Array.Clear(byteOut, 0, byteOut.Length);
  
                                  HttpContext.Current.Response.Clear();
                                  HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename=\"{0}\"", "BufferDeAudio"));
                                  HttpContext.Current.Response.ContentType = "audio/x-wav";
                                  HttpContext.Current.Response.BinaryWrite(pcmData);
                                  HttpContext.Current.Response.End();             
                              }
                          }
                      }
                  }
                  while (oClient.GetStream().DataAvailable == true);
  
                  ns.Close();
              }
              catch (Exception)
              {
  
              }
  
              HttpContext.Current.Response.End();
          }
      }
  
      private void write(NetworkStream ns, string message)
      {
          byte[] msg = Encoding.ASCII.GetBytes(message + Environment.NewLine);
          ns.Write(msg, 0, msg.Length);
      }
  
      private string read(NetworkStream ns)
      {
          StringBuilder sb = new StringBuilder();
  
          if (ns.CanRead)
          {
              byte[] readBuffer = new byte[1024];
  
              int numBytesRead = 0;
  
              do
              {
                  numBytesRead = ns.Read(readBuffer, 0, readBuffer.Length);
                  sb.AppendFormat("{0}", Encoding.ASCII.GetString(readBuffer, 0, numBytesRead));
                  sb.Replace(Convert.ToChar(24), ' ');
                  sb.Replace(Convert.ToChar(255), ' ');
                  sb.Replace('?', ' ');
              }
  
              while (ns.DataAvailable);
          }
  
          return sb.ToString();
          }                                        
  
      public bool IsReusable
      {
          get
          {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>Then, as the loop receives the data line by line, I am sending to the player. But the only sound I hear is the last line of the loop.</p>
  
  <p>I tried to put the player in a thread without success.</p>
  
  <p>Is this possible? Any idea?</p>