title: Simple C# server to stream video over HTTP
body: <p>I am trying to create a simple console application in C #. I need to make a streaming video and it must be accessible directly from the browser. </p>
  
  <p>I was reviewing the link <a href="https://stackoverflow.com/questions/16862782/streaming-large-video-files-net">Streaming large video files .net</a>, but it seems that this code is for asp.net</p>
  
  <p>Another thing that i was trying, is TCPCliente, but I have problems when sending large files.</p>
  
  <pre><code>var fs = File.Open("../../video.mp4", FileMode.Open);
  
  outHeaders.Add("Content-Type", "video/mp4");
  outHeaders.Add("Accept-Ranges", "0-" + fs.Length);
  
  long size = fs.Length;
  long length = size;
  long startBytes = 0;
  long endBytes = size - 1;
  
  if (headers["Range"] != null)
  {
      var msg = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 206 Partial Content" + "\n");
      stream.Write(msg, 0, msg.Length);
  
      var range = ParseRange(size, headers["Range"].ToString());
  
      startBytes = range.Start;
      endBytes = range.End;
  
      fs.Seek(startBytes, SeekOrigin.Begin);
      length = endBytes - startBytes + 1;
  }
  else
  {
      var msg = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK" + "\n");
      stream.Write(msg, 0, msg.Length);
  }
  
  outHeaders.Add("Content-Length", length);
  outHeaders.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startBytes, endBytes, size));
  
  SendHeader(stream, outHeaders);
  
  int bytesRead = 0;
  byte[] buf = new byte[client.SendBufferSize];
  while (fs.Position &lt;= endBytes &amp;&amp; 
      (bytesRead = fs.Read(buf, 0, buf.Length)) &gt; 0)
  {
      stream.Write(buf, 0, bytesRead);
      stream.Flush();
  }
  
  fs.Close();
  </code></pre>
  
  <p>the problem is in:</p>
  
  <pre><code>while (fs.Position &lt;= endBytes &amp;&amp; 
      (bytesRead = fs.Read(buf, 0, buf.Length)) &gt; 0)
  {
      stream.Write(buf, 0, bytesRead);
      stream.Flush();
  }
  </code></pre>
  
  <p>when fs.Position = 425984 I get the error:</p>
  
  <pre><code>No se controló System.IO.IOException
    HResult=-2146232800
    Message=No se pueden leer los datos de la conexión de transporte: Se ha forzado la interrupción de una conexión existente por el host remoto.
    Source=System
    StackTrace:
         en System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
         en HttpServer.Program.Main(String[] args) en j:\HttpServer\HttpServer\Program.cs:línea 85
         en System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         en System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         en System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.Net.Sockets.SocketException
         HResult=-2147467259
         Message=Se ha forzado la interrupción de una conexión existente por el host remoto
         Source=System
         ErrorCode=10054
         NativeErrorCode=10054
         StackTrace:
              en System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
              en System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
         InnerException: 
  </code></pre>
  
  <p>in line <code>stream.Write(buf, 0, bytesRead);</code></p>
  
  <p>What can i do? 
  thx</p>