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.
I was reviewing the link Streaming large video files .net, but it seems that this code is for asp.net
Another thing that i was trying, is TCPCliente, but I have problems when sending large files.
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 <= endBytes &&
(bytesRead = fs.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, bytesRead);
stream.Flush();
}
fs.Close();
the problem is in:
while (fs.Position <= endBytes &&
(bytesRead = fs.Read(buf, 0, buf.Length)) > 0)
{
stream.Write(buf, 0, bytesRead);
stream.Flush();
}
when fs.Position = 425984 I get the error:
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:
in line stream.Write(buf, 0, bytesRead);
What can i do? thx